#INTRO: LOADING PACKAGES ETC----
library(foreign)
library(reshape2)
library(data.table)
library(stargazer)
library(MatchIt)
#library(nonrandom)
library(ggplot2)
#library(gdata1)
library(gmodels)
library(gridExtra)
library(haven)
library(plyr)
#library(car)
#library(xlsx)
library(tidyr)
library(reshape)
library(countrycode)
#library(dGlyr)
library(readr)
library(gnm)
library(MNP)
library(nls2)
library(nlstools)
library(stats)
library(dplyr)
library(matrixStats)
library(miceadds)
library(minpack.lm)
library(lfe)
library(lubridate)
library(AER)
library(reshape)
library(reshape2)
library(tictoc)
library(xtable)
library(Formula)
library(mlogit)
library(plm)
library(survival)
library(plotly)
library(alpaca)
library(lmtest)
library(sandwich)
# library(mnlogit)

library(foreach)#parallel foreach
library(iterators)
library(parallel)
library(doParallel)

library(msm)
library(tikzDevice)

library(openxlsx)#read.xlsx

library(labelled)#change labels to columns

options(width=1000)#set max characters shown
options(max.print=100000)#set max rows shown console

# !diagnostics suppress=Dsvr,Dgdm

rm(list=ls())
cat("\014")


#READ DATA:----

#read TEV:
dt=read_sav("ZA5054_stacked_v1-0-0.sav", encoding = "latin1")

#read de wide:
load("d29_CSES-EV-LP.RData")#de wide (before making the long dataset)

#prepare TEV for variable manipulation:
dt=dt[!duplicated(dt$RRESPID),]#keep one observation by respondent

colnames(dt)[colnames(dt)=="COUNTRY"]="Ec"
dt$Ec=as.character(dt$Ec)
dt$Ec[dt$Ec=="1"]="AUT"#rename country code:
dt$Ec[dt$Ec=="4"]="HRV"
dt$Ec[dt$Ec=="7"]="EST"
dt$Ec[dt$Ec=="8"]="FIN"
dt$Ec[dt$Ec=="10"]="DEU"
dt$Ec[dt$Ec=="11"]="GRC"
dt$Ec[dt$Ec=="12"]="HUN"
dt$Ec[dt$Ec=="13"]="ISL"
dt$Ec[dt$Ec=="14"]="IRL"
dt$Ec[dt$Ec=="15"]="ITA"
dt$Ec[dt$Ec=="16"]="LTU"
dt$Ec[dt$Ec=="17"]="NLD"
dt$Ec[dt$Ec=="18"]="NOR"
dt$Ec[dt$Ec=="19"]="POL"
dt$Ec[dt$Ec=="20"]="PRT"
dt$Ec[dt$Ec=="21"]="ROU"
dt$Ec[dt$Ec=="22"]="SRB"
dt$Ec[dt$Ec=="23"]="SVN"
dt$Ec[dt$Ec=="24"]="ESP"
dt$Ec[dt$Ec=="25"]="SWE"
dt$Ec[dt$Ec=="26"]="CHE"
dt$Ec[dt$Ec=="27"]="GBR"
dt$Ec[dt$Ec=="28"]="TUR"

colnames(dt)[colnames(dt)=="YEAR"]="Ey"

dt$Es=paste0(dt$Ec,"_",dt$Ey)

colnames(dt)[colnames(dt)=="RAGE"]="Rage"#Respondent Age
dt$Rage=as.numeric(dt$Rage)
dt$Rage[dt$Rage>900]=NA

colnames(dt)[colnames(dt)=="RPART1"]="Vt"
dt$Vt=as.character(dt$Vt)
dt$Vt[dt$Vt!="1"&dt$Vt!="2"]=NA
dt$Vt[dt$Vt=="1"]="0"
dt$Vt[dt$Vt=="2"]="1"
dt$Vt=as.numeric(dt$Vt)

colnames(dt)[colnames(dt)=="RVOTE1"]="Vn"
dt$Vn=as.numeric(dt$Vn)
dt$Vn[dt$Vn>14]=NA#not coded Pl, Ll for these Vn (need to keep only Vn with STACKID)

#exclude East Germany in order to have West Germany as congruent electorate (and exclude Eastern Europe):
dt=dt[!(dt$Ec=="DEU"&dt$Ey>1990&dt$RREGION>10),]
#NB: in DEU_1990 even if the election included former East Germany, the survey did not

#Spain:
#set party like Convergence and Union as NA outside Cataluna:
# it is not in the top parties coded in 1979 1982 1993 2004
#set party like Basque Nationalist Party as NA outside Navarra and Pais Vasco:
# it is not in the top parties coded in 1979 1982 1993 2004
#set party like Republican Left of Catalonia as NA outside Cataluna and Comunidad Valenciana:
# it is not in the top parties coded in 1979 1982 1993 2004

#exclude Greek elections GRC_1989j for which there are only two parties coded, and keep in
# GRC_1989 only the November election:
dt=dt[!(dt$RSTUDYID==1119891),]

sort(unique(dew$Es))
sort(unique(dt$Es))


#Va (re-code vote according to party size):----
dt$Va=NA

dt$Va[dt$Es=="ESP_1979"&dt$Vn==1]=1
dt$Va[dt$Es=="ESP_1979"&dt$Vn==2]=2
dt$Va[dt$Es=="ESP_1979"&dt$Vn==3]=3
dt$Va[dt$Es=="ESP_1979"&dt$Vn==4]=4

dt$Va[dt$Es=="ESP_1982"&dt$Vn==3]=1
dt$Va[dt$Es=="ESP_1982"&dt$Vn==2]=2
dt$Va[dt$Es=="ESP_1982"&dt$Vn==4]=3
dt$Va[dt$Es=="ESP_1982"&dt$Vn==1]=4
dt$Va[dt$Es=="ESP_1982"&dt$Vn==5]=5

dt$Va[dt$Es=="ESP_1993"&dt$Vn==1]=1
dt$Va[dt$Es=="ESP_1993"&dt$Vn==3]=2
dt$Va[dt$Es=="ESP_1993"&dt$Vn==4]=3
dt$Va[dt$Es=="ESP_1993"&dt$Vn==2]=4

dt$Va[dt$Es=="ESP_2004"&dt$Vn==1]=1
dt$Va[dt$Es=="ESP_2004"&dt$Vn==2]=2
dt$Va[dt$Es=="ESP_2004"&dt$Vn==3]=3

dt$Va[dt$Es=="EST_2011"&dt$Vn==1]=1
dt$Va[dt$Es=="EST_2011"&dt$Vn==2]=2
dt$Va[dt$Es=="EST_2011"&dt$Vn==3]=3
dt$Va[dt$Es=="EST_2011"&dt$Vn==4]=4
dt$Va[dt$Es=="EST_2011"&dt$Vn==6]=5
dt$Va[dt$Es=="EST_2011"&dt$Vn==5]=6

dt$Va[dt$Es=="FIN_2003"&dt$Vn==1]=1
dt$Va[dt$Es=="FIN_2003"&dt$Vn==3]=2
dt$Va[dt$Es=="FIN_2003"&dt$Vn==2]=3
dt$Va[dt$Es=="FIN_2003"&dt$Vn==4]=4
dt$Va[dt$Es=="FIN_2003"&dt$Vn==5]=5
dt$Va[dt$Es=="FIN_2003"&dt$Vn==6]=6

dt$Va[dt$Es=="GBR_2005"&dt$Vn==1]=1
dt$Va[dt$Es=="GBR_2005"&dt$Vn==2]=2
dt$Va[dt$Es=="GBR_2005"&dt$Vn==3]=3

dt$Va[dt$Es=="GRC_1985"&dt$Vn==1]=1
dt$Va[dt$Es=="GRC_1985"&dt$Vn==2]=2
dt$Va[dt$Es=="GRC_1985"&dt$Vn==3]=3
dt$Va[dt$Es=="GRC_1985"&dt$Vn==4]=4

dt$Va[dt$Es=="GRC_1989"&dt$Vn==1]=1
dt$Va[dt$Es=="GRC_1989"&dt$Vn==2]=2
dt$Va[dt$Es=="GRC_1989"&dt$Vn==3]=3

dt$Va[dt$Es=="GRC_1990"&dt$Vn==1]=1
dt$Va[dt$Es=="GRC_1990"&dt$Vn==2]=2
dt$Va[dt$Es=="GRC_1990"&dt$Vn==3]=3

dt$Va[dt$Es=="GRC_1996"&dt$Vn==1]=1
dt$Va[dt$Es=="GRC_1996"&dt$Vn==2]=2
dt$Va[dt$Es=="GRC_1996"&dt$Vn==3]=3
dt$Va[dt$Es=="GRC_1996"&dt$Vn==4]=4
dt$Va[dt$Es=="GRC_1996"&dt$Vn==5]=5

dt$Va[dt$Es=="GRC_2000"&dt$Vn==1]=1
dt$Va[dt$Es=="GRC_2000"&dt$Vn==2]=2
dt$Va[dt$Es=="GRC_2000"&dt$Vn==3]=3
dt$Va[dt$Es=="GRC_2000"&dt$Vn==4]=4

dt$Va[dt$Es=="GRC_2004"&dt$Vn==1]=1
dt$Va[dt$Es=="GRC_2004"&dt$Vn==2]=2
dt$Va[dt$Es=="GRC_2004"&dt$Vn==3]=3
dt$Va[dt$Es=="GRC_2004"&dt$Vn==4]=4

dt$Va[dt$Es=="GRC_2007"&dt$Vn==1]=1
dt$Va[dt$Es=="GRC_2007"&dt$Vn==2]=2
dt$Va[dt$Es=="GRC_2007"&dt$Vn==3]=3
dt$Va[dt$Es=="GRC_2007"&dt$Vn==4]=4
dt$Va[dt$Es=="GRC_2007"&dt$Vn==5]=5

dt$Va[dt$Es=="HUN_1998"&dt$Vn==1]=1
dt$Va[dt$Es=="HUN_1998"&dt$Vn==5]=2
dt$Va[dt$Es=="HUN_1998"&dt$Vn==2]=3
dt$Va[dt$Es=="HUN_1998"&dt$Vn==6]=4
dt$Va[dt$Es=="HUN_1998"&dt$Vn==4]=5
dt$Va[dt$Es=="HUN_1998"&dt$Vn==3]=6

dt$Va[dt$Es=="HUN_2006"&dt$Vn==1]=1
dt$Va[dt$Es=="HUN_2006"&dt$Vn==2]=2
dt$Va[dt$Es=="HUN_2006"&dt$Vn==3]=3
dt$Va[dt$Es=="HUN_2006"&dt$Vn==4]=4

dt$Va[dt$Es=="IRL_2002"&dt$Vn==1]=1
dt$Va[dt$Es=="IRL_2002"&dt$Vn==2]=2
dt$Va[dt$Es=="IRL_2002"&dt$Vn==4]=3
dt$Va[dt$Es=="IRL_2002"&dt$Vn==5]=4
dt$Va[dt$Es=="IRL_2002"&dt$Vn==3]=5
dt$Va[dt$Es=="IRL_2002"&dt$Vn==6]=6

dt$Va[dt$Es=="ISL_1987"&dt$Vn==3]=1
dt$Va[dt$Es=="ISL_1987"&dt$Vn==2]=2
dt$Va[dt$Es=="ISL_1987"&dt$Vn==1]=3
dt$Va[dt$Es=="ISL_1987"&dt$Vn==4]=4
dt$Va[dt$Es=="ISL_1987"&dt$Vn==5]=5

dt$Va[dt$Es=="ISL_1991"&dt$Vn==3]=1
dt$Va[dt$Es=="ISL_1991"&dt$Vn==2]=2
dt$Va[dt$Es=="ISL_1991"&dt$Vn==1]=3
dt$Va[dt$Es=="ISL_1991"&dt$Vn==4]=4
dt$Va[dt$Es=="ISL_1991"&dt$Vn==5]=5

dt$Va[dt$Es=="ISL_1995"&dt$Vn==3]=1
dt$Va[dt$Es=="ISL_1995"&dt$Vn==2]=2
dt$Va[dt$Es=="ISL_1995"&dt$Vn==4]=3
dt$Va[dt$Es=="ISL_1995"&dt$Vn==1]=4
dt$Va[dt$Es=="ISL_1995"&dt$Vn==5]=5
dt$Va[dt$Es=="ISL_1995"&dt$Vn==6]=6

dt$Va[dt$Es=="ISL_2003"&dt$Vn==3]=1
dt$Va[dt$Es=="ISL_2003"&dt$Vn==1]=2
dt$Va[dt$Es=="ISL_2003"&dt$Vn==2]=3
dt$Va[dt$Es=="ISL_2003"&dt$Vn==4]=4
dt$Va[dt$Es=="ISL_2003"&dt$Vn==5]=5

dt$Va[dt$Es=="ITA_2006"&dt$Vn==3]=1#coalition==1
dt$Va[dt$Es=="ITA_2006"&dt$Vn==5]=2
dt$Va[dt$Es=="ITA_2006"&dt$Vn==6]=3
dt$Va[dt$Es=="ITA_2006"&dt$Vn==4]=4
dt$Va[dt$Es=="ITA_2006"&dt$Vn==1]=5
dt$Va[dt$Es=="ITA_2006"&dt$Vn==7]=6

dt$Va[dt$Es=="ITA_2008"&dt$Vn==4]=1#coalition==1
dt$Va[dt$Es=="ITA_2008"&dt$Vn==2]=2
dt$Va[dt$Es=="ITA_2008"&dt$Vn==5]=3
dt$Va[dt$Es=="ITA_2008"&dt$Vn==6]=4
dt$Va[dt$Es=="ITA_2008"&dt$Vn==3]=5
dt$Va[dt$Es=="ITA_2008"&dt$Vn==1]=6

dt$Va[dt$Es=="LTU_2000"&dt$Vn==2]=1
dt$Va[dt$Es=="LTU_2000"&dt$Vn==3]=2
dt$Va[dt$Es=="LTU_2000"&dt$Vn==4]=3
dt$Va[dt$Es=="LTU_2000"&dt$Vn==1]=4

dt$Va[dt$Es=="NLD_2002"&dt$Vn==2]=1
dt$Va[dt$Es=="NLD_2002"&dt$Vn==6]=2
dt$Va[dt$Es=="NLD_2002"&dt$Vn==3]=3
dt$Va[dt$Es=="NLD_2002"&dt$Vn==1]=4
dt$Va[dt$Es=="NLD_2002"&dt$Vn==5]=5
dt$Va[dt$Es=="NLD_2002"&dt$Vn==7]=6

dt$Va[dt$Es=="NOR_2001"&dt$Vn==2]=1
dt$Va[dt$Es=="NOR_2001"&dt$Vn==6]=2
dt$Va[dt$Es=="NOR_2001"&dt$Vn==7]=3
dt$Va[dt$Es=="NOR_2001"&dt$Vn==1]=4
dt$Va[dt$Es=="NOR_2001"&dt$Vn==4]=5
dt$Va[dt$Es=="NOR_2001"&dt$Vn==5]=6

dt$Va[dt$Es=="PRT_2005"&dt$Vn==5]=1
dt$Va[dt$Es=="PRT_2005"&dt$Vn==4]=2
dt$Va[dt$Es=="PRT_2005"&dt$Vn==3]=3
dt$Va[dt$Es=="PRT_2005"&dt$Vn==2]=4
dt$Va[dt$Es=="PRT_2005"&dt$Vn==1]=5

dt$Va[dt$Es=="SVN_2000"&dt$Vn==2]=1
dt$Va[dt$Es=="SVN_2000"&dt$Vn==5]=2
dt$Va[dt$Es=="SVN_2000"&dt$Vn==7]=3
dt$Va[dt$Es=="SVN_2000"&dt$Vn==3]=4
dt$Va[dt$Es=="SVN_2000"&dt$Vn==6]=5
dt$Va[dt$Es=="SVN_2000"&dt$Vn==1]=6

dt$Va[dt$Es=="SWE_1979"&dt$Vn==2]=1
dt$Va[dt$Es=="SWE_1979"&dt$Vn==5]=2
dt$Va[dt$Es=="SWE_1979"&dt$Vn==3]=3
dt$Va[dt$Es=="SWE_1979"&dt$Vn==4]=4
dt$Va[dt$Es=="SWE_1979"&dt$Vn==1]=5

dt$Va[dt$Es=="SWE_2002"&dt$Vn==2]=1
dt$Va[dt$Es=="SWE_2002"&dt$Vn==5]=2
dt$Va[dt$Es=="SWE_2002"&dt$Vn==4]=3
dt$Va[dt$Es=="SWE_2002"&dt$Vn==6]=4
dt$Va[dt$Es=="SWE_2002"&dt$Vn==1]=5
dt$Va[dt$Es=="SWE_2002"&dt$Vn==3]=6

dt$Va[dt$Es=="SWE_2010"&dt$Vn==2]=1
dt$Va[dt$Es=="SWE_2010"&dt$Vn==5]=2
dt$Va[dt$Es=="SWE_2010"&dt$Vn==7]=3
dt$Va[dt$Es=="SWE_2010"&dt$Vn==4]=4
dt$Va[dt$Es=="SWE_2010"&dt$Vn==3]=5
dt$Va[dt$Es=="SWE_2010"&dt$Vn==10]=6

#common elections TEV-CSES (minus exclusions, see "check common elections"):
dt$Va[dt$Es=="DEU_1961"&dt$Vn==2]=1
dt$Va[dt$Es=="DEU_1961"&dt$Vn==1]=2
dt$Va[dt$Es=="DEU_1961"&dt$Vn==3]=3

dt$Va[dt$Es=="DEU_1965"&dt$Vn==2]=1
dt$Va[dt$Es=="DEU_1965"&dt$Vn==1]=2
dt$Va[dt$Es=="DEU_1965"&dt$Vn==3]=3

dt$Va[dt$Es=="DEU_1976"&dt$Vn==2]=1
dt$Va[dt$Es=="DEU_1976"&dt$Vn==1]=2
dt$Va[dt$Es=="DEU_1976"&dt$Vn==3]=3

dt$Va[dt$Es=="DEU_1980"&dt$Vn==2]=1
dt$Va[dt$Es=="DEU_1980"&dt$Vn==1]=2
dt$Va[dt$Es=="DEU_1980"&dt$Vn==3]=3

dt$Va[dt$Es=="DEU_1983"&dt$Vn==2]=1
dt$Va[dt$Es=="DEU_1983"&dt$Vn==1]=2
dt$Va[dt$Es=="DEU_1983"&dt$Vn==3]=3

dt$Va[dt$Es=="DEU_1987"&dt$Vn==2]=1
dt$Va[dt$Es=="DEU_1987"&dt$Vn==1]=2
dt$Va[dt$Es=="DEU_1987"&dt$Vn==3]=3

dt$Va[dt$Es=="DEU_1990"&dt$Vn==2]=1
dt$Va[dt$Es=="DEU_1990"&dt$Vn==1]=2
dt$Va[dt$Es=="DEU_1990"&dt$Vn==3]=3
dt$Va[dt$Es=="DEU_1990"&dt$Vn==5]=4

dt$Va[dt$Es=="DEU_1994"&dt$Vn==2]=1
dt$Va[dt$Es=="DEU_1994"&dt$Vn==1]=2
dt$Va[dt$Es=="DEU_1994"&dt$Vn==4]=3
dt$Va[dt$Es=="DEU_1994"&dt$Vn==3]=4
dt$Va[dt$Es=="DEU_1994"&dt$Vn==5]=5

dt$Va[dt$Es=="DEU_1998"&dt$Vn==1]=1
dt$Va[dt$Es=="DEU_1998"&dt$Vn==2]=2
dt$Va[dt$Es=="DEU_1998"&dt$Vn==4]=3
dt$Va[dt$Es=="DEU_1998"&dt$Vn==3]=4
dt$Va[dt$Es=="DEU_1998"&dt$Vn==5]=5

dt$Va[dt$Es=="DEU_2009"&dt$Vn==2]=1
dt$Va[dt$Es=="DEU_2009"&dt$Vn==1]=2
dt$Va[dt$Es=="DEU_2009"&dt$Vn==3]=3
dt$Va[dt$Es=="DEU_2009"&dt$Vn==5]=4
dt$Va[dt$Es=="DEU_2009"&dt$Vn==4]=5

dt$Va[dt$Es=="DEU_2013"&dt$Vn==2]=1
dt$Va[dt$Es=="DEU_2013"&dt$Vn==1]=2
dt$Va[dt$Es=="DEU_2013"&dt$Vn==5]=3
dt$Va[dt$Es=="DEU_2013"&dt$Vn==4]=4
dt$Va[dt$Es=="DEU_2013"&dt$Vn==3]=5

dt$Va[dt$Es=="FIN_2007"&dt$Vn==1]=1
dt$Va[dt$Es=="FIN_2007"&dt$Vn==3]=2
dt$Va[dt$Es=="FIN_2007"&dt$Vn==2]=3
dt$Va[dt$Es=="FIN_2007"&dt$Vn==4]=4
dt$Va[dt$Es=="FIN_2007"&dt$Vn==5]=5
dt$Va[dt$Es=="FIN_2007"&dt$Vn==7]=6

dt$Va[dt$Es=="FIN_2011"&dt$Vn==3]=1
dt$Va[dt$Es=="FIN_2011"&dt$Vn==2]=2
dt$Va[dt$Es=="FIN_2011"&dt$Vn==8]=3
dt$Va[dt$Es=="FIN_2011"&dt$Vn==1]=4
dt$Va[dt$Es=="FIN_2011"&dt$Vn==4]=5
dt$Va[dt$Es=="FIN_2011"&dt$Vn==5]=6

dt$Va[dt$Es=="GRC_2009"&dt$Vn==1]=1
dt$Va[dt$Es=="GRC_2009"&dt$Vn==2]=2
dt$Va[dt$Es=="GRC_2009"&dt$Vn==3]=3
dt$Va[dt$Es=="GRC_2009"&dt$Vn==4]=4
dt$Va[dt$Es=="GRC_2009"&dt$Vn==5]=5
dt$Va[dt$Es=="GRC_2009"&dt$Vn==6]=6

dt$Va[dt$Es=="GRC_2012"&dt$Vn==1]=1
dt$Va[dt$Es=="GRC_2012"&dt$Vn==2]=2
dt$Va[dt$Es=="GRC_2012"&dt$Vn==3]=3
dt$Va[dt$Es=="GRC_2012"&dt$Vn==4]=4
dt$Va[dt$Es=="GRC_2012"&dt$Vn==5]=5
dt$Va[dt$Es=="GRC_2012"&dt$Vn==6]=6

dt$Va[dt$Es=="IRL_2007"&dt$Vn==1]=1
dt$Va[dt$Es=="IRL_2007"&dt$Vn==2]=2
dt$Va[dt$Es=="IRL_2007"&dt$Vn==4]=3
dt$Va[dt$Es=="IRL_2007"&dt$Vn==3]=4
dt$Va[dt$Es=="IRL_2007"&dt$Vn==6]=5
dt$Va[dt$Es=="IRL_2007"&dt$Vn==5]=6

dt$Va[dt$Es=="IRL_2011"&dt$Vn==2]=1
dt$Va[dt$Es=="IRL_2011"&dt$Vn==3]=2
dt$Va[dt$Es=="IRL_2011"&dt$Vn==1]=3
dt$Va[dt$Es=="IRL_2011"&dt$Vn==4]=4
dt$Va[dt$Es=="IRL_2011"&dt$Vn==5]=5#

dt$Va[dt$Es=="ISL_1999"&dt$Vn==3]=1
dt$Va[dt$Es=="ISL_1999"&dt$Vn==1]=2
dt$Va[dt$Es=="ISL_1999"&dt$Vn==2]=3
dt$Va[dt$Es=="ISL_1999"&dt$Vn==4]=4
dt$Va[dt$Es=="ISL_1999"&dt$Vn==5]=5

dt$Va[dt$Es=="ISL_2007"&dt$Vn==3]=1
dt$Va[dt$Es=="ISL_2007"&dt$Vn==1]=2
dt$Va[dt$Es=="ISL_2007"&dt$Vn==4]=3
dt$Va[dt$Es=="ISL_2007"&dt$Vn==2]=4
dt$Va[dt$Es=="ISL_2007"&dt$Vn==5]=5
dt$Va[dt$Es=="ISL_2007"&dt$Vn==6]=6#

dt$Va[dt$Es=="ISL_2009"&dt$Vn==1]=1
dt$Va[dt$Es=="ISL_2009"&dt$Vn==3]=2
dt$Va[dt$Es=="ISL_2009"&dt$Vn==4]=3
dt$Va[dt$Es=="ISL_2009"&dt$Vn==2]=4
dt$Va[dt$Es=="ISL_2009"&dt$Vn==5]=5#
dt$Va[dt$Es=="ISL_2009"&dt$Vn==6]=6#

dt$Va[dt$Es=="ISL_2013"&dt$Vn==3]=1
dt$Va[dt$Es=="ISL_2013"&dt$Vn==2]=2
dt$Va[dt$Es=="ISL_2013"&dt$Vn==1]=3
dt$Va[dt$Es=="ISL_2013"&dt$Vn==4]=4
dt$Va[dt$Es=="ISL_2013"&dt$Vn==5]=5
dt$Va[dt$Es=="ISL_2013"&dt$Vn==6]=6

dt$Va[dt$Es=="NLD_1994"&dt$Vn==1]=1
dt$Va[dt$Es=="NLD_1994"&dt$Vn==2]=2
dt$Va[dt$Es=="NLD_1994"&dt$Vn==3]=3
dt$Va[dt$Es=="NLD_1994"&dt$Vn==4]=4

dt$Va[dt$Es=="NLD_1998"&dt$Vn==1]=1#EV 4 parties, CSES 6 parties coded
dt$Va[dt$Es=="NLD_1998"&dt$Vn==3]=2
dt$Va[dt$Es=="NLD_1998"&dt$Vn==2]=3
dt$Va[dt$Es=="NLD_1998"&dt$Vn==4]=4
dt$Va[dt$Es=="NLD_1998"&dt$Vn==5]=5
dt$Va[dt$Es=="NLD_1998"&dt$Vn==7]=6#

dt$Va[dt$Es=="NOR_1981"&dt$Vn==2]=1
dt$Va[dt$Es=="NOR_1981"&dt$Vn==6]=2
dt$Va[dt$Es=="NOR_1981"&dt$Vn==4]=3
dt$Va[dt$Es=="NOR_1981"&dt$Vn==5]=4
dt$Va[dt$Es=="NOR_1981"&dt$Vn==1]=5
dt$Va[dt$Es=="NOR_1981"&dt$Vn==7]=6

dt$Va[dt$Es=="NOR_1985"&dt$Vn==2]=1
dt$Va[dt$Es=="NOR_1985"&dt$Vn==6]=2
dt$Va[dt$Es=="NOR_1985"&dt$Vn==4]=3
dt$Va[dt$Es=="NOR_1985"&dt$Vn==5]=4
dt$Va[dt$Es=="NOR_1985"&dt$Vn==1]=5
dt$Va[dt$Es=="NOR_1985"&dt$Vn==7]=6

dt$Va[dt$Es=="NOR_1989"&dt$Vn==2]=1
dt$Va[dt$Es=="NOR_1989"&dt$Vn==6]=2
dt$Va[dt$Es=="NOR_1989"&dt$Vn==7]=3
dt$Va[dt$Es=="NOR_1989"&dt$Vn==1]=4
dt$Va[dt$Es=="NOR_1989"&dt$Vn==4]=5
dt$Va[dt$Es=="NOR_1989"&dt$Vn==5]=6

dt$Va[dt$Es=="NOR_1993"&dt$Vn==2]=1
dt$Va[dt$Es=="NOR_1993"&dt$Vn==5]=2
dt$Va[dt$Es=="NOR_1993"&dt$Vn==6]=3
dt$Va[dt$Es=="NOR_1993"&dt$Vn==1]=4
dt$Va[dt$Es=="NOR_1993"&dt$Vn==4]=5
dt$Va[dt$Es=="NOR_1993"&dt$Vn==7]=6

dt$Va[dt$Es=="NOR_1997"&dt$Vn==2]=1
dt$Va[dt$Es=="NOR_1997"&dt$Vn==7]=2
dt$Va[dt$Es=="NOR_1997"&dt$Vn==4]=3
dt$Va[dt$Es=="NOR_1997"&dt$Vn==6]=4
dt$Va[dt$Es=="NOR_1997"&dt$Vn==5]=5
dt$Va[dt$Es=="NOR_1997"&dt$Vn==1]=6

dt$Va[dt$Es=="NOR_2005"&dt$Vn==2]=1
dt$Va[dt$Es=="NOR_2005"&dt$Vn==7]=2
dt$Va[dt$Es=="NOR_2005"&dt$Vn==6]=3
dt$Va[dt$Es=="NOR_2005"&dt$Vn==1]=4
dt$Va[dt$Es=="NOR_2005"&dt$Vn==4]=5
dt$Va[dt$Es=="NOR_2005"&dt$Vn==5]=6

dt$Va[dt$Es=="PRT_2002"&dt$Vn==4]=1
dt$Va[dt$Es=="PRT_2002"&dt$Vn==5]=2
dt$Va[dt$Es=="PRT_2002"&dt$Vn==2]=3
dt$Va[dt$Es=="PRT_2002"&dt$Vn==3]=4
dt$Va[dt$Es=="PRT_2002"&dt$Vn==1]=5
dt$Va[dt$Es=="PRT_2002"&dt$Vn==6]=6

dt$Va[dt$Es=="PRT_2009"&dt$Vn==5]=1#osym4-5  inverted!?
dt$Va[dt$Es=="PRT_2009"&dt$Vn==4]=2
dt$Va[dt$Es=="PRT_2009"&dt$Vn==2]=3
dt$Va[dt$Es=="PRT_2009"&dt$Vn==1]=4
dt$Va[dt$Es=="PRT_2009"&dt$Vn==3]=5

dt$Va[dt$Es=="SWE_1982"&dt$Vn==2]=1
dt$Va[dt$Es=="SWE_1982"&dt$Vn==5]=2
dt$Va[dt$Es=="SWE_1982"&dt$Vn==3]=3
dt$Va[dt$Es=="SWE_1982"&dt$Vn==4]=4
dt$Va[dt$Es=="SWE_1982"&dt$Vn==1]=5

dt$Va[dt$Es=="SWE_1985"&dt$Vn==2]=1
dt$Va[dt$Es=="SWE_1985"&dt$Vn==5]=2
dt$Va[dt$Es=="SWE_1985"&dt$Vn==4]=3
dt$Va[dt$Es=="SWE_1985"&dt$Vn==3]=4
dt$Va[dt$Es=="SWE_1985"&dt$Vn==1]=5
dt$Va[dt$Es=="SWE_1985"&dt$Vn==6]=6

dt$Va[dt$Es=="SWE_1988"&dt$Vn==2]=1
dt$Va[dt$Es=="SWE_1988"&dt$Vn==5]=2
dt$Va[dt$Es=="SWE_1988"&dt$Vn==4]=3
dt$Va[dt$Es=="SWE_1988"&dt$Vn==3]=4
dt$Va[dt$Es=="SWE_1988"&dt$Vn==1]=5

dt$Va[dt$Es=="SWE_1991"&dt$Vn==2]=1
dt$Va[dt$Es=="SWE_1991"&dt$Vn==5]=2
dt$Va[dt$Es=="SWE_1991"&dt$Vn==4]=3
dt$Va[dt$Es=="SWE_1991"&dt$Vn==3]=4
dt$Va[dt$Es=="SWE_1991"&dt$Vn==6]=5
dt$Va[dt$Es=="SWE_1991"&dt$Vn==9]=6

dt$Va[dt$Es=="SWE_1994"&dt$Vn==2]=1
dt$Va[dt$Es=="SWE_1994"&dt$Vn==5]=2
dt$Va[dt$Es=="SWE_1994"&dt$Vn==3]=3
dt$Va[dt$Es=="SWE_1994"&dt$Vn==4]=4
dt$Va[dt$Es=="SWE_1994"&dt$Vn==1]=5
dt$Va[dt$Es=="SWE_1994"&dt$Vn==7]=6

dt$Va[dt$Es=="SWE_1998"&dt$Vn==2]=1
dt$Va[dt$Es=="SWE_1998"&dt$Vn==5]=2
dt$Va[dt$Es=="SWE_1998"&dt$Vn==1]=3
dt$Va[dt$Es=="SWE_1998"&dt$Vn==6]=4
dt$Va[dt$Es=="SWE_1998"&dt$Vn==3]=5
dt$Va[dt$Es=="SWE_1998"&dt$Vn==4]=6

dt$Va[dt$Es=="SWE_2006"&dt$Vn==2]=1
dt$Va[dt$Es=="SWE_2006"&dt$Vn==5]=2
dt$Va[dt$Es=="SWE_2006"&dt$Vn==3]=3
dt$Va[dt$Es=="SWE_2006"&dt$Vn==4]=4
dt$Va[dt$Es=="SWE_2006"&dt$Vn==6]=5
dt$Va[dt$Es=="SWE_2006"&dt$Vn==1]=6


#party likability:----
dt$Pl.1=NA#create Pl:
dt$Pl.2=NA
dt$Pl.3=NA
dt$Pl.4=NA
dt$Pl.5=NA
dt$Pl.6=NA

dt$Pl.1[dt$Es=="ESP_1979"]=dt$RSYM1[dt$Es=="ESP_1979"]#assign Pl by party size order for parties with likability:
dt$Pl.2[dt$Es=="ESP_1979"]=dt$RSYM2[dt$Es=="ESP_1979"]
dt$Pl.3[dt$Es=="ESP_1979"]=dt$RSYM3[dt$Es=="ESP_1979"]
dt$Pl.4[dt$Es=="ESP_1979"]=dt$RSYM4[dt$Es=="ESP_1979"]

dt$Pl.1[dt$Es=="ESP_1982"]=dt$RSYM3[dt$Es=="ESP_1982"]
dt$Pl.2[dt$Es=="ESP_1982"]=dt$RSYM2[dt$Es=="ESP_1982"]
dt$Pl.3[dt$Es=="ESP_1982"]=dt$RSYM4[dt$Es=="ESP_1982"]
dt$Pl.4[dt$Es=="ESP_1982"]=dt$RSYM1[dt$Es=="ESP_1982"]
dt$Pl.5[dt$Es=="ESP_1982"]=dt$RSYM5[dt$Es=="ESP_1982"]

dt$Pl.1[dt$Es=="ESP_1993"]=dt$RSYM1[dt$Es=="ESP_1993"]
dt$Pl.2[dt$Es=="ESP_1993"]=dt$RSYM3[dt$Es=="ESP_1993"]
dt$Pl.3[dt$Es=="ESP_1993"]=dt$RSYM4[dt$Es=="ESP_1993"]
dt$Pl.4[dt$Es=="ESP_1993"]=dt$RSYM2[dt$Es=="ESP_1993"]

dt$Pl.1[dt$Es=="ESP_2004"]=dt$RSYM1[dt$Es=="ESP_2004"]
dt$Pl.2[dt$Es=="ESP_2004"]=dt$RSYM2[dt$Es=="ESP_2004"]
dt$Pl.3[dt$Es=="ESP_2004"]=dt$RSYM3[dt$Es=="ESP_2004"]

dt$Pl.1[dt$Es=="EST_2011"]=dt$RSYM1[dt$Es=="EST_2011"]
dt$Pl.2[dt$Es=="EST_2011"]=dt$RSYM2[dt$Es=="EST_2011"]
dt$Pl.3[dt$Es=="EST_2011"]=dt$RSYM3[dt$Es=="EST_2011"]
dt$Pl.4[dt$Es=="EST_2011"]=dt$RSYM4[dt$Es=="EST_2011"]
dt$Pl.5[dt$Es=="EST_2011"]=dt$RSYM6[dt$Es=="EST_2011"]
dt$Pl.6[dt$Es=="EST_2011"]=dt$RSYM5[dt$Es=="EST_2011"]

dt$Pl.1[dt$Es=="FIN_2003"]=dt$RSYM1[dt$Es=="FIN_2003"]
dt$Pl.2[dt$Es=="FIN_2003"]=dt$RSYM3[dt$Es=="FIN_2003"]
dt$Pl.3[dt$Es=="FIN_2003"]=dt$RSYM2[dt$Es=="FIN_2003"]
dt$Pl.4[dt$Es=="FIN_2003"]=dt$RSYM4[dt$Es=="FIN_2003"]
dt$Pl.5[dt$Es=="FIN_2003"]=dt$RSYM5[dt$Es=="FIN_2003"]
dt$Pl.6[dt$Es=="FIN_2003"]=dt$RSYM6[dt$Es=="FIN_2003"]

dt$Pl.1[dt$Es=="GBR_2005"]=dt$RSYM1[dt$Es=="GBR_2005"]
dt$Pl.2[dt$Es=="GBR_2005"]=dt$RSYM2[dt$Es=="GBR_2005"]
dt$Pl.3[dt$Es=="GBR_2005"]=dt$RSYM3[dt$Es=="GBR_2005"]

dt$Pl.1[dt$Es=="GRC_1985"]=dt$RSYM1[dt$Es=="GRC_1985"]
dt$Pl.2[dt$Es=="GRC_1985"]=dt$RSYM2[dt$Es=="GRC_1985"]
dt$Pl.3[dt$Es=="GRC_1985"]=dt$RSYM3[dt$Es=="GRC_1985"]
dt$Pl.4[dt$Es=="GRC_1985"]=dt$RSYM4[dt$Es=="GRC_1985"]

dt$Pl.1[dt$Es=="GRC_1989"]=dt$RSYM1[dt$Es=="GRC_1989"]
dt$Pl.2[dt$Es=="GRC_1989"]=dt$RSYM2[dt$Es=="GRC_1989"]
dt$Pl.3[dt$Es=="GRC_1989"]=dt$RSYM3[dt$Es=="GRC_1989"]

dt$Pl.1[dt$Es=="GRC_1990"]=dt$RSYM1[dt$Es=="GRC_1990"]
dt$Pl.2[dt$Es=="GRC_1990"]=dt$RSYM2[dt$Es=="GRC_1990"]
dt$Pl.3[dt$Es=="GRC_1990"]=dt$RSYM3[dt$Es=="GRC_1990"]

dt$Pl.1[dt$Es=="GRC_1996"]=dt$RSYM1[dt$Es=="GRC_1996"]
dt$Pl.2[dt$Es=="GRC_1996"]=dt$RSYM2[dt$Es=="GRC_1996"]
dt$Pl.3[dt$Es=="GRC_1996"]=dt$RSYM3[dt$Es=="GRC_1996"]
dt$Pl.4[dt$Es=="GRC_1996"]=dt$RSYM4[dt$Es=="GRC_1996"]
dt$Pl.5[dt$Es=="GRC_1996"]=dt$RSYM5[dt$Es=="GRC_1996"]

dt$Pl.1[dt$Es=="GRC_2000"]=dt$RSYM1[dt$Es=="GRC_2000"]
dt$Pl.2[dt$Es=="GRC_2000"]=dt$RSYM2[dt$Es=="GRC_2000"]
dt$Pl.3[dt$Es=="GRC_2000"]=dt$RSYM3[dt$Es=="GRC_2000"]
dt$Pl.4[dt$Es=="GRC_2000"]=dt$RSYM4[dt$Es=="GRC_2000"]

dt$Pl.1[dt$Es=="GRC_2004"]=dt$RSYM1[dt$Es=="GRC_2004"]
dt$Pl.2[dt$Es=="GRC_2004"]=dt$RSYM2[dt$Es=="GRC_2004"]
dt$Pl.3[dt$Es=="GRC_2004"]=dt$RSYM3[dt$Es=="GRC_2004"]
dt$Pl.4[dt$Es=="GRC_2004"]=dt$RSYM4[dt$Es=="GRC_2004"]

dt$Pl.1[dt$Es=="GRC_2007"]=dt$RSYM1[dt$Es=="GRC_2007"]
dt$Pl.2[dt$Es=="GRC_2007"]=dt$RSYM2[dt$Es=="GRC_2007"]
dt$Pl.3[dt$Es=="GRC_2007"]=dt$RSYM3[dt$Es=="GRC_2007"]
dt$Pl.4[dt$Es=="GRC_2007"]=dt$RSYM4[dt$Es=="GRC_2007"]
dt$Pl.5[dt$Es=="GRC_2007"]=dt$RSYM5[dt$Es=="GRC_2007"]

dt$Pl.1[dt$Es=="HUN_1998"]=dt$RSYM1[dt$Es=="HUN_1998"]
dt$Pl.2[dt$Es=="HUN_1998"]=dt$RSYM5[dt$Es=="HUN_1998"]
dt$Pl.3[dt$Es=="HUN_1998"]=dt$RSYM2[dt$Es=="HUN_1998"]
dt$Pl.4[dt$Es=="HUN_1998"]=dt$RSYM6[dt$Es=="HUN_1998"]
dt$Pl.5[dt$Es=="HUN_1998"]=dt$RSYM4[dt$Es=="HUN_1998"]
dt$Pl.6[dt$Es=="HUN_1998"]=dt$RSYM3[dt$Es=="HUN_1998"]

dt$Pl.1[dt$Es=="HUN_2006"]=dt$RSYM1[dt$Es=="HUN_2006"]
dt$Pl.2[dt$Es=="HUN_2006"]=dt$RSYM2[dt$Es=="HUN_2006"]
dt$Pl.3[dt$Es=="HUN_2006"]=dt$RSYM3[dt$Es=="HUN_2006"]
dt$Pl.4[dt$Es=="HUN_2006"]=dt$RSYM4[dt$Es=="HUN_2006"]

dt$Pl.1[dt$Es=="IRL_2002"]=dt$RSYM1[dt$Es=="IRL_2002"]
dt$Pl.2[dt$Es=="IRL_2002"]=dt$RSYM2[dt$Es=="IRL_2002"]
dt$Pl.3[dt$Es=="IRL_2002"]=dt$RSYM4[dt$Es=="IRL_2002"]
dt$Pl.4[dt$Es=="IRL_2002"]=dt$RSYM5[dt$Es=="IRL_2002"]
dt$Pl.5[dt$Es=="IRL_2002"]=dt$RSYM3[dt$Es=="IRL_2002"]
dt$Pl.6[dt$Es=="IRL_2002"]=dt$RSYM6[dt$Es=="IRL_2002"]

dt$Pl.1[dt$Es=="ISL_1987"]=dt$RSYM3[dt$Es=="ISL_1987"]
dt$Pl.2[dt$Es=="ISL_1987"]=dt$RSYM2[dt$Es=="ISL_1987"]
dt$Pl.3[dt$Es=="ISL_1987"]=dt$RSYM1[dt$Es=="ISL_1987"]
dt$Pl.4[dt$Es=="ISL_1987"]=dt$RSYM4[dt$Es=="ISL_1987"]
dt$Pl.5[dt$Es=="ISL_1987"]=dt$RSYM5[dt$Es=="ISL_1987"]

dt$Pl.1[dt$Es=="ISL_1991"]=dt$RSYM3[dt$Es=="ISL_1991"]
dt$Pl.2[dt$Es=="ISL_1991"]=dt$RSYM2[dt$Es=="ISL_1991"]
dt$Pl.3[dt$Es=="ISL_1991"]=dt$RSYM1[dt$Es=="ISL_1991"]
dt$Pl.4[dt$Es=="ISL_1991"]=dt$RSYM4[dt$Es=="ISL_1991"]
dt$Pl.5[dt$Es=="ISL_1991"]=dt$RSYM5[dt$Es=="ISL_1991"]

dt$Pl.1[dt$Es=="ISL_1995"]=dt$RSYM3[dt$Es=="ISL_1995"]
dt$Pl.2[dt$Es=="ISL_1995"]=dt$RSYM2[dt$Es=="ISL_1995"]
dt$Pl.3[dt$Es=="ISL_1995"]=dt$RSYM4[dt$Es=="ISL_1995"]
dt$Pl.4[dt$Es=="ISL_1995"]=dt$RSYM1[dt$Es=="ISL_1995"]
dt$Pl.5[dt$Es=="ISL_1995"]=dt$RSYM5[dt$Es=="ISL_1995"]
dt$Pl.6[dt$Es=="ISL_1995"]=dt$RSYM6[dt$Es=="ISL_1995"]

dt$Pl.1[dt$Es=="ISL_2003"]=dt$RSYM3[dt$Es=="ISL_2003"]
dt$Pl.2[dt$Es=="ISL_2003"]=dt$RSYM1[dt$Es=="ISL_2003"]
dt$Pl.3[dt$Es=="ISL_2003"]=dt$RSYM2[dt$Es=="ISL_2003"]
dt$Pl.4[dt$Es=="ISL_2003"]=dt$RSYM4[dt$Es=="ISL_2003"]
dt$Pl.5[dt$Es=="ISL_2003"]=dt$RSYM5[dt$Es=="ISL_2003"]

dt$Pl.1[dt$Es=="ITA_2006"]=dt$RSYM3[dt$Es=="ITA_2006"]#coalition==1
dt$Pl.2[dt$Es=="ITA_2006"]=dt$RSYM5[dt$Es=="ITA_2006"]
dt$Pl.3[dt$Es=="ITA_2006"]=dt$RSYM6[dt$Es=="ITA_2006"]
dt$Pl.4[dt$Es=="ITA_2006"]=dt$RSYM4[dt$Es=="ITA_2006"]
dt$Pl.5[dt$Es=="ITA_2006"]=dt$RSYM1[dt$Es=="ITA_2006"]
dt$Pl.6[dt$Es=="ITA_2006"]=dt$RSYM7[dt$Es=="ITA_2006"]

dt$Pl.1[dt$Es=="ITA_2008"]=dt$RSYM4[dt$Es=="ITA_2008"]#coalition==1
dt$Pl.2[dt$Es=="ITA_2008"]=dt$RSYM2[dt$Es=="ITA_2008"]
dt$Pl.3[dt$Es=="ITA_2008"]=dt$RSYM5[dt$Es=="ITA_2008"]
dt$Pl.4[dt$Es=="ITA_2008"]=dt$RSYM6[dt$Es=="ITA_2008"]
dt$Pl.5[dt$Es=="ITA_2008"]=dt$RSYM3[dt$Es=="ITA_2008"]
dt$Pl.6[dt$Es=="ITA_2008"]=dt$RSYM1[dt$Es=="ITA_2008"]

dt$Pl.1[dt$Es=="LTU_2000"]=dt$RSYM2[dt$Es=="LTU_2000"]
dt$Pl.2[dt$Es=="LTU_2000"]=dt$RSYM3[dt$Es=="LTU_2000"]
dt$Pl.3[dt$Es=="LTU_2000"]=dt$RSYM4[dt$Es=="LTU_2000"]
dt$Pl.4[dt$Es=="LTU_2000"]=dt$RSYM1[dt$Es=="LTU_2000"]

dt$Pl.1[dt$Es=="NLD_2002"]=dt$RSYM2[dt$Es=="NLD_2002"]
dt$Pl.2[dt$Es=="NLD_2002"]=dt$RSYM6[dt$Es=="NLD_2002"]
dt$Pl.3[dt$Es=="NLD_2002"]=dt$RSYM3[dt$Es=="NLD_2002"]
dt$Pl.4[dt$Es=="NLD_2002"]=dt$RSYM1[dt$Es=="NLD_2002"]
dt$Pl.5[dt$Es=="NLD_2002"]=dt$RSYM5[dt$Es=="NLD_2002"]
dt$Pl.6[dt$Es=="NLD_2002"]=dt$RSYM7[dt$Es=="NLD_2002"]

dt$Pl.1[dt$Es=="NOR_2001"]=dt$RSYM2[dt$Es=="NOR_2001"]
dt$Pl.2[dt$Es=="NOR_2001"]=dt$RSYM6[dt$Es=="NOR_2001"]
dt$Pl.3[dt$Es=="NOR_2001"]=dt$RSYM7[dt$Es=="NOR_2001"]
dt$Pl.4[dt$Es=="NOR_2001"]=dt$RSYM1[dt$Es=="NOR_2001"]
dt$Pl.5[dt$Es=="NOR_2001"]=dt$RSYM4[dt$Es=="NOR_2001"]
dt$Pl.6[dt$Es=="NOR_2001"]=dt$RSYM5[dt$Es=="NOR_2001"]

dt$Pl.1[dt$Es=="PRT_2005"]=dt$RSYM5[dt$Es=="PRT_2005"]
dt$Pl.2[dt$Es=="PRT_2005"]=dt$RSYM4[dt$Es=="PRT_2005"]
dt$Pl.3[dt$Es=="PRT_2005"]=dt$RSYM3[dt$Es=="PRT_2005"]
dt$Pl.4[dt$Es=="PRT_2005"]=dt$RSYM2[dt$Es=="PRT_2005"]
dt$Pl.5[dt$Es=="PRT_2005"]=dt$RSYM1[dt$Es=="PRT_2005"]

dt$Pl.1[dt$Es=="SVN_2000"]=dt$RSYM2[dt$Es=="SVN_2000"]
dt$Pl.2[dt$Es=="SVN_2000"]=dt$RSYM5[dt$Es=="SVN_2000"]
dt$Pl.3[dt$Es=="SVN_2000"]=dt$RSYM7[dt$Es=="SVN_2000"]
dt$Pl.4[dt$Es=="SVN_2000"]=dt$RSYM3[dt$Es=="SVN_2000"]
dt$Pl.5[dt$Es=="SVN_2000"]=dt$RSYM6[dt$Es=="SVN_2000"]
dt$Pl.6[dt$Es=="SVN_2000"]=dt$RSYM1[dt$Es=="SVN_2000"]

dt$Pl.1[dt$Es=="SWE_1979"]=dt$RSYM2[dt$Es=="SWE_1979"]
dt$Pl.2[dt$Es=="SWE_1979"]=dt$RSYM5[dt$Es=="SWE_1979"]
dt$Pl.3[dt$Es=="SWE_1979"]=dt$RSYM3[dt$Es=="SWE_1979"]
dt$Pl.4[dt$Es=="SWE_1979"]=dt$RSYM4[dt$Es=="SWE_1979"]
dt$Pl.5[dt$Es=="SWE_1979"]=dt$RSYM1[dt$Es=="SWE_1979"]

dt$Pl.1[dt$Es=="SWE_2002"]=dt$RSYM2[dt$Es=="SWE_2002"]
dt$Pl.2[dt$Es=="SWE_2002"]=dt$RSYM5[dt$Es=="SWE_2002"]
dt$Pl.3[dt$Es=="SWE_2002"]=dt$RSYM4[dt$Es=="SWE_2002"]
dt$Pl.4[dt$Es=="SWE_2002"]=dt$RSYM6[dt$Es=="SWE_2002"]
dt$Pl.5[dt$Es=="SWE_2002"]=dt$RSYM1[dt$Es=="SWE_2002"]
dt$Pl.6[dt$Es=="SWE_2002"]=dt$RSYM3[dt$Es=="SWE_2002"]

dt$Pl.1[dt$Es=="SWE_2010"]=dt$RSYM2[dt$Es=="SWE_2010"]
dt$Pl.2[dt$Es=="SWE_2010"]=dt$RSYM5[dt$Es=="SWE_2010"]
dt$Pl.3[dt$Es=="SWE_2010"]=dt$RSYM7[dt$Es=="SWE_2010"]
dt$Pl.4[dt$Es=="SWE_2010"]=dt$RSYM4[dt$Es=="SWE_2010"]
dt$Pl.5[dt$Es=="SWE_2010"]=dt$RSYM3[dt$Es=="SWE_2010"]
dt$Pl.6[dt$Es=="SWE_2010"]=dt$RSYM10[dt$Es=="SWE_2010"]

#common elections TEV-CSES (minus exclusions, see "check common elections"):
dt$Pl.1[dt$Es=="DEU_1961"]=dt$RSYM2[dt$Es=="DEU_1961"]
dt$Pl.2[dt$Es=="DEU_1961"]=dt$RSYM1[dt$Es=="DEU_1961"]
dt$Pl.3[dt$Es=="DEU_1961"]=dt$RSYM3[dt$Es=="DEU_1961"]

dt$Pl.1[dt$Es=="DEU_1965"]=dt$RSYM2[dt$Es=="DEU_1965"]
dt$Pl.2[dt$Es=="DEU_1965"]=dt$RSYM1[dt$Es=="DEU_1965"]
dt$Pl.3[dt$Es=="DEU_1965"]=dt$RSYM3[dt$Es=="DEU_1965"]

dt$Pl.1[dt$Es=="DEU_1976"]=dt$RSYM2[dt$Es=="DEU_1976"]
dt$Pl.2[dt$Es=="DEU_1976"]=dt$RSYM1[dt$Es=="DEU_1976"]
dt$Pl.3[dt$Es=="DEU_1976"]=dt$RSYM3[dt$Es=="DEU_1976"]

dt$Pl.1[dt$Es=="DEU_1980"]=dt$RSYM2[dt$Es=="DEU_1980"]
dt$Pl.2[dt$Es=="DEU_1980"]=dt$RSYM1[dt$Es=="DEU_1980"]
dt$Pl.3[dt$Es=="DEU_1980"]=dt$RSYM3[dt$Es=="DEU_1980"]

dt$Pl.1[dt$Es=="DEU_1983"]=dt$RSYM2[dt$Es=="DEU_1983"]
dt$Pl.2[dt$Es=="DEU_1983"]=dt$RSYM1[dt$Es=="DEU_1983"]
dt$Pl.3[dt$Es=="DEU_1983"]=dt$RSYM3[dt$Es=="DEU_1983"]

dt$Pl.1[dt$Es=="DEU_1987"]=dt$RSYM2[dt$Es=="DEU_1987"]
dt$Pl.2[dt$Es=="DEU_1987"]=dt$RSYM1[dt$Es=="DEU_1987"]
dt$Pl.3[dt$Es=="DEU_1987"]=dt$RSYM3[dt$Es=="DEU_1987"]

dt$Pl.1[dt$Es=="DEU_1990"]=dt$RSYM2[dt$Es=="DEU_1990"]
dt$Pl.2[dt$Es=="DEU_1990"]=dt$RSYM1[dt$Es=="DEU_1990"]
dt$Pl.3[dt$Es=="DEU_1990"]=dt$RSYM3[dt$Es=="DEU_1990"]
dt$Pl.4[dt$Es=="DEU_1990"]=dt$RSYM5[dt$Es=="DEU_1990"]

dt$Pl.1[dt$Es=="DEU_1994"]=dt$RSYM2[dt$Es=="DEU_1994"]
dt$Pl.2[dt$Es=="DEU_1994"]=dt$RSYM1[dt$Es=="DEU_1994"]
dt$Pl.3[dt$Es=="DEU_1994"]=dt$RSYM4[dt$Es=="DEU_1994"]
dt$Pl.4[dt$Es=="DEU_1994"]=dt$RSYM3[dt$Es=="DEU_1994"]
dt$Pl.5[dt$Es=="DEU_1994"]=dt$RSYM5[dt$Es=="DEU_1994"]

dt$Pl.1[dt$Es=="DEU_1998"]=dt$RSYM1[dt$Es=="DEU_1998"]
dt$Pl.2[dt$Es=="DEU_1998"]=dt$RSYM2[dt$Es=="DEU_1998"]
dt$Pl.3[dt$Es=="DEU_1998"]=dt$RSYM4[dt$Es=="DEU_1998"]
dt$Pl.4[dt$Es=="DEU_1998"]=dt$RSYM3[dt$Es=="DEU_1998"]
dt$Pl.5[dt$Es=="DEU_1998"]=dt$RSYM5[dt$Es=="DEU_1998"]

dt$Pl.1[dt$Es=="DEU_2009"]=dt$RSYM2[dt$Es=="DEU_2009"]
dt$Pl.2[dt$Es=="DEU_2009"]=dt$RSYM1[dt$Es=="DEU_2009"]
dt$Pl.3[dt$Es=="DEU_2009"]=dt$RSYM3[dt$Es=="DEU_2009"]
dt$Pl.4[dt$Es=="DEU_2009"]=dt$RSYM5[dt$Es=="DEU_2009"]
dt$Pl.5[dt$Es=="DEU_2009"]=dt$RSYM4[dt$Es=="DEU_2009"]

dt$Pl.1[dt$Es=="DEU_2013"]=dt$RSYM2[dt$Es=="DEU_2013"]
dt$Pl.2[dt$Es=="DEU_2013"]=dt$RSYM1[dt$Es=="DEU_2013"]
dt$Pl.3[dt$Es=="DEU_2013"]=dt$RSYM5[dt$Es=="DEU_2013"]
dt$Pl.4[dt$Es=="DEU_2013"]=dt$RSYM4[dt$Es=="DEU_2013"]
dt$Pl.5[dt$Es=="DEU_2013"]=dt$RSYM3[dt$Es=="DEU_2013"]

dt$Pl.1[dt$Es=="FIN_2007"]=dt$RSYM1[dt$Es=="FIN_2007"]
dt$Pl.2[dt$Es=="FIN_2007"]=dt$RSYM3[dt$Es=="FIN_2007"]
dt$Pl.3[dt$Es=="FIN_2007"]=dt$RSYM2[dt$Es=="FIN_2007"]
dt$Pl.4[dt$Es=="FIN_2007"]=dt$RSYM4[dt$Es=="FIN_2007"]
dt$Pl.5[dt$Es=="FIN_2007"]=dt$RSYM5[dt$Es=="FIN_2007"]
dt$Pl.6[dt$Es=="FIN_2007"]=dt$RSYM7[dt$Es=="FIN_2007"]

dt$Pl.1[dt$Es=="FIN_2011"]=dt$RSYM3[dt$Es=="FIN_2011"]
dt$Pl.2[dt$Es=="FIN_2011"]=dt$RSYM2[dt$Es=="FIN_2011"]
dt$Pl.3[dt$Es=="FIN_2011"]=dt$RSYM8[dt$Es=="FIN_2011"]
dt$Pl.4[dt$Es=="FIN_2011"]=dt$RSYM1[dt$Es=="FIN_2011"]
dt$Pl.5[dt$Es=="FIN_2011"]=dt$RSYM4[dt$Es=="FIN_2011"]
dt$Pl.6[dt$Es=="FIN_2011"]=dt$RSYM5[dt$Es=="FIN_2011"]

dt$Pl.1[dt$Es=="GRC_2009"]=dt$RSYM1[dt$Es=="GRC_2009"]
dt$Pl.2[dt$Es=="GRC_2009"]=dt$RSYM2[dt$Es=="GRC_2009"]
dt$Pl.3[dt$Es=="GRC_2009"]=dt$RSYM3[dt$Es=="GRC_2009"]
dt$Pl.4[dt$Es=="GRC_2009"]=dt$RSYM4[dt$Es=="GRC_2009"]
dt$Pl.5[dt$Es=="GRC_2009"]=dt$RSYM5[dt$Es=="GRC_2009"]
dt$Pl.6[dt$Es=="GRC_2009"]=dt$RSYM6[dt$Es=="GRC_2009"]

dt$Pl.1[dt$Es=="GRC_2012"]=dt$RSYM1[dt$Es=="GRC_2012"]
dt$Pl.2[dt$Es=="GRC_2012"]=dt$RSYM2[dt$Es=="GRC_2012"]
dt$Pl.3[dt$Es=="GRC_2012"]=dt$RSYM3[dt$Es=="GRC_2012"]
dt$Pl.4[dt$Es=="GRC_2012"]=dt$RSYM4[dt$Es=="GRC_2012"]
dt$Pl.5[dt$Es=="GRC_2012"]=dt$RSYM5[dt$Es=="GRC_2012"]
dt$Pl.6[dt$Es=="GRC_2012"]=dt$RSYM6[dt$Es=="GRC_2012"]

dt$Pl.1[dt$Es=="IRL_2007"]=dt$RSYM1[dt$Es=="IRL_2007"]
dt$Pl.2[dt$Es=="IRL_2007"]=dt$RSYM2[dt$Es=="IRL_2007"]
dt$Pl.3[dt$Es=="IRL_2007"]=dt$RSYM4[dt$Es=="IRL_2007"]
dt$Pl.4[dt$Es=="IRL_2007"]=dt$RSYM3[dt$Es=="IRL_2007"]
dt$Pl.5[dt$Es=="IRL_2007"]=dt$RSYM6[dt$Es=="IRL_2007"]
dt$Pl.6[dt$Es=="IRL_2007"]=dt$RSYM5[dt$Es=="IRL_2007"]

dt$Pl.1[dt$Es=="IRL_2011"]=dt$RSYM2[dt$Es=="IRL_2011"]
dt$Pl.2[dt$Es=="IRL_2011"]=dt$RSYM3[dt$Es=="IRL_2011"]
dt$Pl.3[dt$Es=="IRL_2011"]=dt$RSYM1[dt$Es=="IRL_2011"]
dt$Pl.4[dt$Es=="IRL_2011"]=dt$RSYM4[dt$Es=="IRL_2011"]
dt$Pl.5[dt$Es=="IRL_2011"]=dt$RSYM5[dt$Es=="IRL_2011"]#

dt$Pl.1[dt$Es=="ISL_1999"]=dt$RSYM3[dt$Es=="ISL_1999"]
dt$Pl.2[dt$Es=="ISL_1999"]=dt$RSYM1[dt$Es=="ISL_1999"]
dt$Pl.3[dt$Es=="ISL_1999"]=dt$RSYM2[dt$Es=="ISL_1999"]
dt$Pl.4[dt$Es=="ISL_1999"]=dt$RSYM4[dt$Es=="ISL_1999"]
dt$Pl.5[dt$Es=="ISL_1999"]=dt$RSYM5[dt$Es=="ISL_1999"]

dt$Pl.1[dt$Es=="ISL_2007"]=dt$RSYM3[dt$Es=="ISL_2007"]
dt$Pl.2[dt$Es=="ISL_2007"]=dt$RSYM1[dt$Es=="ISL_2007"]
dt$Pl.3[dt$Es=="ISL_2007"]=dt$RSYM4[dt$Es=="ISL_2007"]
dt$Pl.4[dt$Es=="ISL_2007"]=dt$RSYM2[dt$Es=="ISL_2007"]
dt$Pl.5[dt$Es=="ISL_2007"]=dt$RSYM5[dt$Es=="ISL_2007"]
dt$Pl.6[dt$Es=="ISL_2007"]=dt$RSYM6[dt$Es=="ISL_2007"]#

dt$Pl.1[dt$Es=="ISL_2009"]=dt$RSYM1[dt$Es=="ISL_2009"]
dt$Pl.2[dt$Es=="ISL_2009"]=dt$RSYM3[dt$Es=="ISL_2009"]
dt$Pl.3[dt$Es=="ISL_2009"]=dt$RSYM4[dt$Es=="ISL_2009"]
dt$Pl.4[dt$Es=="ISL_2009"]=dt$RSYM2[dt$Es=="ISL_2009"]
dt$Pl.5[dt$Es=="ISL_2009"]=dt$RSYM5[dt$Es=="ISL_2009"]#
dt$Pl.6[dt$Es=="ISL_2009"]=dt$RSYM6[dt$Es=="ISL_2009"]#

dt$Pl.1[dt$Es=="ISL_2013"]=dt$RSYM3[dt$Es=="ISL_2013"]
dt$Pl.2[dt$Es=="ISL_2013"]=dt$RSYM2[dt$Es=="ISL_2013"]
dt$Pl.3[dt$Es=="ISL_2013"]=dt$RSYM1[dt$Es=="ISL_2013"]
dt$Pl.4[dt$Es=="ISL_2013"]=dt$RSYM4[dt$Es=="ISL_2013"]
dt$Pl.5[dt$Es=="ISL_2013"]=dt$RSYM5[dt$Es=="ISL_2013"]
dt$Pl.6[dt$Es=="ISL_2013"]=dt$RSYM6[dt$Es=="ISL_2013"]

dt$Pl.1[dt$Es=="NLD_1994"]=dt$RSYM1[dt$Es=="NLD_1994"]
dt$Pl.2[dt$Es=="NLD_1994"]=dt$RSYM2[dt$Es=="NLD_1994"]
dt$Pl.3[dt$Es=="NLD_1994"]=dt$RSYM3[dt$Es=="NLD_1994"]
dt$Pl.4[dt$Es=="NLD_1994"]=dt$RSYM4[dt$Es=="NLD_1994"]

dt$Pl.1[dt$Es=="NLD_1998"]=dt$RSYM1[dt$Es=="NLD_1998"]#EV 4 parties, CSES 6 parties coded
dt$Pl.2[dt$Es=="NLD_1998"]=dt$RSYM3[dt$Es=="NLD_1998"]
dt$Pl.3[dt$Es=="NLD_1998"]=dt$RSYM2[dt$Es=="NLD_1998"]
dt$Pl.4[dt$Es=="NLD_1998"]=dt$RSYM4[dt$Es=="NLD_1998"]
dt$Pl.5[dt$Es=="NLD_1998"]=dt$RSYM5[dt$Es=="NLD_1998"]
dt$Pl.6[dt$Es=="NLD_1998"]=dt$RSYM7[dt$Es=="NLD_1998"]#

dt$Pl.1[dt$Es=="NOR_1981"]=dt$RSYM2[dt$Es=="NOR_1981"]
dt$Pl.2[dt$Es=="NOR_1981"]=dt$RSYM6[dt$Es=="NOR_1981"]
dt$Pl.3[dt$Es=="NOR_1981"]=dt$RSYM4[dt$Es=="NOR_1981"]
dt$Pl.4[dt$Es=="NOR_1981"]=dt$RSYM5[dt$Es=="NOR_1981"]
dt$Pl.5[dt$Es=="NOR_1981"]=dt$RSYM1[dt$Es=="NOR_1981"]
dt$Pl.6[dt$Es=="NOR_1981"]=dt$RSYM7[dt$Es=="NOR_1981"]

dt$Pl.1[dt$Es=="NOR_1985"]=dt$RSYM2[dt$Es=="NOR_1985"]
dt$Pl.2[dt$Es=="NOR_1985"]=dt$RSYM6[dt$Es=="NOR_1985"]
dt$Pl.3[dt$Es=="NOR_1985"]=dt$RSYM4[dt$Es=="NOR_1985"]
dt$Pl.4[dt$Es=="NOR_1985"]=dt$RSYM5[dt$Es=="NOR_1985"]
dt$Pl.5[dt$Es=="NOR_1985"]=dt$RSYM1[dt$Es=="NOR_1985"]
dt$Pl.6[dt$Es=="NOR_1985"]=dt$RSYM7[dt$Es=="NOR_1985"]

dt$Pl.1[dt$Es=="NOR_1989"]=dt$RSYM2[dt$Es=="NOR_1989"]
dt$Pl.2[dt$Es=="NOR_1989"]=dt$RSYM6[dt$Es=="NOR_1989"]
dt$Pl.3[dt$Es=="NOR_1989"]=dt$RSYM7[dt$Es=="NOR_1989"]
dt$Pl.4[dt$Es=="NOR_1989"]=dt$RSYM1[dt$Es=="NOR_1989"]
dt$Pl.5[dt$Es=="NOR_1989"]=dt$RSYM4[dt$Es=="NOR_1989"]
dt$Pl.6[dt$Es=="NOR_1989"]=dt$RSYM5[dt$Es=="NOR_1989"]

dt$Pl.1[dt$Es=="NOR_1993"]=dt$RSYM2[dt$Es=="NOR_1993"]
dt$Pl.2[dt$Es=="NOR_1993"]=dt$RSYM5[dt$Es=="NOR_1993"]
dt$Pl.3[dt$Es=="NOR_1993"]=dt$RSYM6[dt$Es=="NOR_1993"]
dt$Pl.4[dt$Es=="NOR_1993"]=dt$RSYM1[dt$Es=="NOR_1993"]
dt$Pl.5[dt$Es=="NOR_1993"]=dt$RSYM4[dt$Es=="NOR_1993"]
dt$Pl.6[dt$Es=="NOR_1993"]=dt$RSYM7[dt$Es=="NOR_1993"]

dt$Pl.1[dt$Es=="NOR_1997"]=dt$RSYM2[dt$Es=="NOR_1997"]
dt$Pl.2[dt$Es=="NOR_1997"]=dt$RSYM7[dt$Es=="NOR_1997"]
dt$Pl.3[dt$Es=="NOR_1997"]=dt$RSYM4[dt$Es=="NOR_1997"]
dt$Pl.4[dt$Es=="NOR_1997"]=dt$RSYM6[dt$Es=="NOR_1997"]
dt$Pl.5[dt$Es=="NOR_1997"]=dt$RSYM5[dt$Es=="NOR_1997"]
dt$Pl.6[dt$Es=="NOR_1997"]=dt$RSYM1[dt$Es=="NOR_1997"]

dt$Pl.1[dt$Es=="NOR_2005"]=dt$RSYM2[dt$Es=="NOR_2005"]
dt$Pl.2[dt$Es=="NOR_2005"]=dt$RSYM7[dt$Es=="NOR_2005"]
dt$Pl.3[dt$Es=="NOR_2005"]=dt$RSYM6[dt$Es=="NOR_2005"]
dt$Pl.4[dt$Es=="NOR_2005"]=dt$RSYM1[dt$Es=="NOR_2005"]
dt$Pl.5[dt$Es=="NOR_2005"]=dt$RSYM4[dt$Es=="NOR_2005"]
dt$Pl.6[dt$Es=="NOR_2005"]=dt$RSYM5[dt$Es=="NOR_2005"]

dt$Pl.1[dt$Es=="PRT_2002"]=dt$RSYM4[dt$Es=="PRT_2002"]
dt$Pl.2[dt$Es=="PRT_2002"]=dt$RSYM5[dt$Es=="PRT_2002"]
dt$Pl.3[dt$Es=="PRT_2002"]=dt$RSYM2[dt$Es=="PRT_2002"]
dt$Pl.4[dt$Es=="PRT_2002"]=dt$RSYM3[dt$Es=="PRT_2002"]
dt$Pl.5[dt$Es=="PRT_2002"]=dt$RSYM1[dt$Es=="PRT_2002"]
dt$Pl.6[dt$Es=="PRT_2002"]=dt$RSYM6[dt$Es=="PRT_2002"]

dt$Pl.1[dt$Es=="PRT_2009"]=dt$RSYM5[dt$Es=="PRT_2009"]#osym4-5  inverted!?
dt$Pl.2[dt$Es=="PRT_2009"]=dt$RSYM4[dt$Es=="PRT_2009"]
dt$Pl.3[dt$Es=="PRT_2009"]=dt$RSYM2[dt$Es=="PRT_2009"]
dt$Pl.4[dt$Es=="PRT_2009"]=dt$RSYM1[dt$Es=="PRT_2009"]
dt$Pl.5[dt$Es=="PRT_2009"]=dt$RSYM3[dt$Es=="PRT_2009"]

dt$Pl.1[dt$Es=="SWE_1982"]=dt$RSYM2[dt$Es=="SWE_1982"]
dt$Pl.2[dt$Es=="SWE_1982"]=dt$RSYM5[dt$Es=="SWE_1982"]
dt$Pl.3[dt$Es=="SWE_1982"]=dt$RSYM3[dt$Es=="SWE_1982"]
dt$Pl.4[dt$Es=="SWE_1982"]=dt$RSYM4[dt$Es=="SWE_1982"]
dt$Pl.5[dt$Es=="SWE_1982"]=dt$RSYM1[dt$Es=="SWE_1982"]

dt$Pl.1[dt$Es=="SWE_1985"]=dt$RSYM2[dt$Es=="SWE_1985"]
dt$Pl.2[dt$Es=="SWE_1985"]=dt$RSYM5[dt$Es=="SWE_1985"]
dt$Pl.3[dt$Es=="SWE_1985"]=dt$RSYM4[dt$Es=="SWE_1985"]
dt$Pl.4[dt$Es=="SWE_1985"]=dt$RSYM3[dt$Es=="SWE_1985"]
dt$Pl.5[dt$Es=="SWE_1985"]=dt$RSYM1[dt$Es=="SWE_1985"]
dt$Pl.6[dt$Es=="SWE_1985"]=dt$RSYM6[dt$Es=="SWE_1985"]

dt$Pl.1[dt$Es=="SWE_1988"]=dt$RSYM2[dt$Es=="SWE_1988"]
dt$Pl.2[dt$Es=="SWE_1988"]=dt$RSYM5[dt$Es=="SWE_1988"]
dt$Pl.3[dt$Es=="SWE_1988"]=dt$RSYM4[dt$Es=="SWE_1988"]
dt$Pl.4[dt$Es=="SWE_1988"]=dt$RSYM3[dt$Es=="SWE_1988"]
dt$Pl.5[dt$Es=="SWE_1988"]=dt$RSYM1[dt$Es=="SWE_1988"]

dt$Pl.1[dt$Es=="SWE_1991"]=dt$RSYM2[dt$Es=="SWE_1991"]
dt$Pl.2[dt$Es=="SWE_1991"]=dt$RSYM5[dt$Es=="SWE_1991"]
dt$Pl.3[dt$Es=="SWE_1991"]=dt$RSYM4[dt$Es=="SWE_1991"]
dt$Pl.4[dt$Es=="SWE_1991"]=dt$RSYM3[dt$Es=="SWE_1991"]
dt$Pl.5[dt$Es=="SWE_1991"]=dt$RSYM6[dt$Es=="SWE_1991"]
dt$Pl.6[dt$Es=="SWE_1991"]=dt$RSYM9[dt$Es=="SWE_1991"]

dt$Pl.1[dt$Es=="SWE_1994"]=dt$RSYM2[dt$Es=="SWE_1994"]
dt$Pl.2[dt$Es=="SWE_1994"]=dt$RSYM5[dt$Es=="SWE_1994"]
dt$Pl.3[dt$Es=="SWE_1994"]=dt$RSYM3[dt$Es=="SWE_1994"]
dt$Pl.4[dt$Es=="SWE_1994"]=dt$RSYM4[dt$Es=="SWE_1994"]
dt$Pl.5[dt$Es=="SWE_1994"]=dt$RSYM1[dt$Es=="SWE_1994"]
dt$Pl.6[dt$Es=="SWE_1994"]=dt$RSYM7[dt$Es=="SWE_1994"]

dt$Pl.1[dt$Es=="SWE_1998"]=dt$RSYM2[dt$Es=="SWE_1998"]
dt$Pl.2[dt$Es=="SWE_1998"]=dt$RSYM5[dt$Es=="SWE_1998"]
dt$Pl.3[dt$Es=="SWE_1998"]=dt$RSYM1[dt$Es=="SWE_1998"]
dt$Pl.4[dt$Es=="SWE_1998"]=dt$RSYM6[dt$Es=="SWE_1998"]
dt$Pl.5[dt$Es=="SWE_1998"]=dt$RSYM3[dt$Es=="SWE_1998"]
dt$Pl.6[dt$Es=="SWE_1998"]=dt$RSYM4[dt$Es=="SWE_1998"]

dt$Pl.1[dt$Es=="SWE_2006"]=dt$RSYM2[dt$Es=="SWE_2006"]
dt$Pl.2[dt$Es=="SWE_2006"]=dt$RSYM5[dt$Es=="SWE_2006"]
dt$Pl.3[dt$Es=="SWE_2006"]=dt$RSYM3[dt$Es=="SWE_2006"]
dt$Pl.4[dt$Es=="SWE_2006"]=dt$RSYM4[dt$Es=="SWE_2006"]
dt$Pl.5[dt$Es=="SWE_2006"]=dt$RSYM6[dt$Es=="SWE_2006"]
dt$Pl.6[dt$Es=="SWE_2006"]=dt$RSYM1[dt$Es=="SWE_2006"]

dt$Pl.1=as.numeric(dt$Pl.1)#recode as numeric:
dt$Pl.2=as.numeric(dt$Pl.2)
dt$Pl.3=as.numeric(dt$Pl.3)
dt$Pl.4=as.numeric(dt$Pl.4)
dt$Pl.5=as.numeric(dt$Pl.5)
dt$Pl.6=as.numeric(dt$Pl.6)

dt$Pl.1[dt$Pl.1>1]=NA#exclude NAs coded 900s:
dt$Pl.2[dt$Pl.2>1]=NA
dt$Pl.3[dt$Pl.3>1]=NA
dt$Pl.4[dt$Pl.4>1]=NA
dt$Pl.5[dt$Pl.5>1]=NA
dt$Pl.6[dt$Pl.6>1]=NA

dt$Pl.1=dt$Pl.1*10#recode to 0-10 scale:
dt$Pl.2=dt$Pl.2*10
dt$Pl.3=dt$Pl.3*10
dt$Pl.4=dt$Pl.4*10
dt$Pl.5=dt$Pl.5*10
dt$Pl.6=dt$Pl.6*10

#leader likability:----
dt$Ll.1=NA#create Ll:
dt$Ll.2=NA
dt$Ll.3=NA
dt$Ll.4=NA
dt$Ll.5=NA
dt$Ll.6=NA

dt$Ll.1[dt$Es=="ESP_1979"]=dt$RSYML1[dt$Es=="ESP_1979"]#assign Ll by party size order for parties with likability:
dt$Ll.2[dt$Es=="ESP_1979"]=dt$RSYML2[dt$Es=="ESP_1979"]
dt$Ll.3[dt$Es=="ESP_1979"]=dt$RSYML3[dt$Es=="ESP_1979"]
dt$Ll.4[dt$Es=="ESP_1979"]=dt$RSYML4[dt$Es=="ESP_1979"]

dt$Ll.1[dt$Es=="ESP_1982"]=dt$RSYML3[dt$Es=="ESP_1982"]
dt$Ll.2[dt$Es=="ESP_1982"]=dt$RSYML2[dt$Es=="ESP_1982"]
dt$Ll.3[dt$Es=="ESP_1982"]=dt$RSYML4[dt$Es=="ESP_1982"]
dt$Ll.4[dt$Es=="ESP_1982"]=dt$RSYML1[dt$Es=="ESP_1982"]
dt$Ll.5[dt$Es=="ESP_1982"]=dt$RSYML5[dt$Es=="ESP_1982"]

dt$Ll.1[dt$Es=="ESP_1993"]=dt$RSYML1[dt$Es=="ESP_1993"]
dt$Ll.2[dt$Es=="ESP_1993"]=dt$RSYML3[dt$Es=="ESP_1993"]
dt$Ll.3[dt$Es=="ESP_1993"]=dt$RSYML4[dt$Es=="ESP_1993"]
dt$Ll.4[dt$Es=="ESP_1993"]=dt$RSYML2[dt$Es=="ESP_1993"]

dt$Ll.1[dt$Es=="ESP_2004"]=dt$RSYML1[dt$Es=="ESP_2004"]
dt$Ll.2[dt$Es=="ESP_2004"]=dt$RSYML2[dt$Es=="ESP_2004"]
dt$Ll.3[dt$Es=="ESP_2004"]=dt$RSYML3[dt$Es=="ESP_2004"]

dt$Ll.1[dt$Es=="EST_2011"]=dt$RSYML1[dt$Es=="EST_2011"]
dt$Ll.2[dt$Es=="EST_2011"]=dt$RSYML2[dt$Es=="EST_2011"]
dt$Ll.3[dt$Es=="EST_2011"]=dt$RSYML3[dt$Es=="EST_2011"]
dt$Ll.4[dt$Es=="EST_2011"]=dt$RSYML4[dt$Es=="EST_2011"]
dt$Ll.5[dt$Es=="EST_2011"]=dt$RSYML6[dt$Es=="EST_2011"]
dt$Ll.6[dt$Es=="EST_2011"]=dt$RSYML5[dt$Es=="EST_2011"]

dt$Ll.1[dt$Es=="FIN_2003"]=dt$RSYML1[dt$Es=="FIN_2003"]
dt$Ll.2[dt$Es=="FIN_2003"]=dt$RSYML3[dt$Es=="FIN_2003"]
dt$Ll.3[dt$Es=="FIN_2003"]=dt$RSYML2[dt$Es=="FIN_2003"]
dt$Ll.4[dt$Es=="FIN_2003"]=dt$RSYML4[dt$Es=="FIN_2003"]
dt$Ll.5[dt$Es=="FIN_2003"]=dt$RSYML5[dt$Es=="FIN_2003"]
dt$Ll.6[dt$Es=="FIN_2003"]=dt$RSYML6[dt$Es=="FIN_2003"]

dt$Ll.1[dt$Es=="GBR_2005"]=dt$RSYML1[dt$Es=="GBR_2005"]
dt$Ll.2[dt$Es=="GBR_2005"]=dt$RSYML2[dt$Es=="GBR_2005"]
dt$Ll.3[dt$Es=="GBR_2005"]=dt$RSYML3[dt$Es=="GBR_2005"]

dt$Ll.1[dt$Es=="GRC_1985"]=dt$RSYML1[dt$Es=="GRC_1985"]
dt$Ll.2[dt$Es=="GRC_1985"]=dt$RSYML2[dt$Es=="GRC_1985"]
dt$Ll.3[dt$Es=="GRC_1985"]=dt$RSYML3[dt$Es=="GRC_1985"]
dt$Ll.4[dt$Es=="GRC_1985"]=dt$RSYML4[dt$Es=="GRC_1985"]

dt$Ll.1[dt$Es=="GRC_1989"]=dt$RSYML1[dt$Es=="GRC_1989"]
dt$Ll.2[dt$Es=="GRC_1989"]=dt$RSYML2[dt$Es=="GRC_1989"]
dt$Ll.3[dt$Es=="GRC_1989"]=dt$RSYML3[dt$Es=="GRC_1989"]

dt$Ll.1[dt$Es=="GRC_1990"]=dt$RSYML1[dt$Es=="GRC_1990"]
dt$Ll.2[dt$Es=="GRC_1990"]=dt$RSYML2[dt$Es=="GRC_1990"]
dt$Ll.3[dt$Es=="GRC_1990"]=dt$RSYML3[dt$Es=="GRC_1990"]

dt$Ll.1[dt$Es=="GRC_1996"]=dt$RSYML1[dt$Es=="GRC_1996"]
dt$Ll.2[dt$Es=="GRC_1996"]=dt$RSYML2[dt$Es=="GRC_1996"]
dt$Ll.3[dt$Es=="GRC_1996"]=dt$RSYML3[dt$Es=="GRC_1996"]
dt$Ll.4[dt$Es=="GRC_1996"]=dt$RSYML4[dt$Es=="GRC_1996"]
dt$Ll.5[dt$Es=="GRC_1996"]=dt$RSYML5[dt$Es=="GRC_1996"]

dt$Ll.1[dt$Es=="GRC_2000"]=dt$RSYML1[dt$Es=="GRC_2000"]
dt$Ll.2[dt$Es=="GRC_2000"]=dt$RSYML2[dt$Es=="GRC_2000"]
dt$Ll.3[dt$Es=="GRC_2000"]=dt$RSYML3[dt$Es=="GRC_2000"]
dt$Ll.4[dt$Es=="GRC_2000"]=dt$RSYML4[dt$Es=="GRC_2000"]

dt$Ll.1[dt$Es=="GRC_2004"]=dt$RSYML1[dt$Es=="GRC_2004"]
dt$Ll.2[dt$Es=="GRC_2004"]=dt$RSYML2[dt$Es=="GRC_2004"]
dt$Ll.3[dt$Es=="GRC_2004"]=dt$RSYML3[dt$Es=="GRC_2004"]
dt$Ll.4[dt$Es=="GRC_2004"]=dt$RSYML4[dt$Es=="GRC_2004"]

dt$Ll.1[dt$Es=="GRC_2007"]=dt$RSYML1[dt$Es=="GRC_2007"]
dt$Ll.2[dt$Es=="GRC_2007"]=dt$RSYML2[dt$Es=="GRC_2007"]
dt$Ll.3[dt$Es=="GRC_2007"]=dt$RSYML3[dt$Es=="GRC_2007"]
dt$Ll.4[dt$Es=="GRC_2007"]=dt$RSYML4[dt$Es=="GRC_2007"]
dt$Ll.5[dt$Es=="GRC_2007"]=dt$RSYML5[dt$Es=="GRC_2007"]

dt$Ll.1[dt$Es=="HUN_1998"]=dt$RSYML1[dt$Es=="HUN_1998"]
dt$Ll.2[dt$Es=="HUN_1998"]=dt$RSYML5[dt$Es=="HUN_1998"]
dt$Ll.3[dt$Es=="HUN_1998"]=dt$RSYML2[dt$Es=="HUN_1998"]
dt$Ll.4[dt$Es=="HUN_1998"]=dt$RSYML6[dt$Es=="HUN_1998"]
dt$Ll.5[dt$Es=="HUN_1998"]=dt$RSYML4[dt$Es=="HUN_1998"]
dt$Ll.6[dt$Es=="HUN_1998"]=dt$RSYML3[dt$Es=="HUN_1998"]

dt$Ll.1[dt$Es=="HUN_2006"]=dt$RSYML1[dt$Es=="HUN_2006"]
dt$Ll.2[dt$Es=="HUN_2006"]=dt$RSYML2[dt$Es=="HUN_2006"]
dt$Ll.3[dt$Es=="HUN_2006"]=dt$RSYML3[dt$Es=="HUN_2006"]
dt$Ll.4[dt$Es=="HUN_2006"]=dt$RSYML4[dt$Es=="HUN_2006"]

dt$Ll.1[dt$Es=="IRL_2002"]=dt$RSYML1[dt$Es=="IRL_2002"]
dt$Ll.2[dt$Es=="IRL_2002"]=dt$RSYML2[dt$Es=="IRL_2002"]
dt$Ll.3[dt$Es=="IRL_2002"]=dt$RSYML4[dt$Es=="IRL_2002"]
dt$Ll.4[dt$Es=="IRL_2002"]=dt$RSYML5[dt$Es=="IRL_2002"]
dt$Ll.5[dt$Es=="IRL_2002"]=dt$RSYML3[dt$Es=="IRL_2002"]
dt$Ll.6[dt$Es=="IRL_2002"]=dt$RSYML6[dt$Es=="IRL_2002"]

dt$Ll.1[dt$Es=="ISL_1987"]=dt$RSYML3[dt$Es=="ISL_1987"]
dt$Ll.2[dt$Es=="ISL_1987"]=dt$RSYML2[dt$Es=="ISL_1987"]
dt$Ll.3[dt$Es=="ISL_1987"]=dt$RSYML1[dt$Es=="ISL_1987"]
dt$Ll.4[dt$Es=="ISL_1987"]=dt$RSYML4[dt$Es=="ISL_1987"]
dt$Ll.5[dt$Es=="ISL_1987"]=dt$RSYML5[dt$Es=="ISL_1987"]

dt$Ll.1[dt$Es=="ISL_1991"]=dt$RSYML3[dt$Es=="ISL_1991"]
dt$Ll.2[dt$Es=="ISL_1991"]=dt$RSYML2[dt$Es=="ISL_1991"]
dt$Ll.3[dt$Es=="ISL_1991"]=dt$RSYML1[dt$Es=="ISL_1991"]
dt$Ll.4[dt$Es=="ISL_1991"]=dt$RSYML4[dt$Es=="ISL_1991"]
dt$Ll.5[dt$Es=="ISL_1991"]=dt$RSYML5[dt$Es=="ISL_1991"]

dt$Ll.1[dt$Es=="ISL_1995"]=dt$RSYML3[dt$Es=="ISL_1995"]
dt$Ll.2[dt$Es=="ISL_1995"]=dt$RSYML2[dt$Es=="ISL_1995"]
dt$Ll.3[dt$Es=="ISL_1995"]=dt$RSYML4[dt$Es=="ISL_1995"]
dt$Ll.4[dt$Es=="ISL_1995"]=dt$RSYML1[dt$Es=="ISL_1995"]
dt$Ll.5[dt$Es=="ISL_1995"]=dt$RSYML5[dt$Es=="ISL_1995"]
dt$Ll.6[dt$Es=="ISL_1995"]=dt$RSYML6[dt$Es=="ISL_1995"]

dt$Ll.1[dt$Es=="ISL_2003"]=dt$RSYML3[dt$Es=="ISL_2003"]
dt$Ll.2[dt$Es=="ISL_2003"]=dt$RSYML1[dt$Es=="ISL_2003"]
dt$Ll.3[dt$Es=="ISL_2003"]=dt$RSYML2[dt$Es=="ISL_2003"]
dt$Ll.4[dt$Es=="ISL_2003"]=dt$RSYML4[dt$Es=="ISL_2003"]
dt$Ll.5[dt$Es=="ISL_2003"]=dt$RSYML5[dt$Es=="ISL_2003"]

dt$Ll.1[dt$Es=="ITA_2006"]=dt$RSYML3[dt$Es=="ITA_2006"]#coalition==1
dt$Ll.2[dt$Es=="ITA_2006"]=dt$RSYML5[dt$Es=="ITA_2006"]
dt$Ll.3[dt$Es=="ITA_2006"]=dt$RSYML6[dt$Es=="ITA_2006"]
dt$Ll.4[dt$Es=="ITA_2006"]=dt$RSYML4[dt$Es=="ITA_2006"]
dt$Ll.5[dt$Es=="ITA_2006"]=dt$RSYML1[dt$Es=="ITA_2006"]
dt$Ll.6[dt$Es=="ITA_2006"]=dt$RSYML7[dt$Es=="ITA_2006"]

dt$Ll.1[dt$Es=="ITA_2008"]=dt$RSYML4[dt$Es=="ITA_2008"]#coalition==1
dt$Ll.2[dt$Es=="ITA_2008"]=dt$RSYML2[dt$Es=="ITA_2008"]
dt$Ll.3[dt$Es=="ITA_2008"]=dt$RSYML5[dt$Es=="ITA_2008"]
dt$Ll.4[dt$Es=="ITA_2008"]=dt$RSYML6[dt$Es=="ITA_2008"]
dt$Ll.5[dt$Es=="ITA_2008"]=dt$RSYML3[dt$Es=="ITA_2008"]
dt$Ll.6[dt$Es=="ITA_2008"]=dt$RSYML1[dt$Es=="ITA_2008"]

dt$Ll.1[dt$Es=="LTU_2000"]=dt$RSYML2[dt$Es=="LTU_2000"]
dt$Ll.2[dt$Es=="LTU_2000"]=dt$RSYML3[dt$Es=="LTU_2000"]
dt$Ll.3[dt$Es=="LTU_2000"]=dt$RSYML4[dt$Es=="LTU_2000"]
dt$Ll.4[dt$Es=="LTU_2000"]=dt$RSYML1[dt$Es=="LTU_2000"]

dt$Ll.1[dt$Es=="NLD_2002"]=dt$RSYML2[dt$Es=="NLD_2002"]
dt$Ll.2[dt$Es=="NLD_2002"]=dt$RSYML6[dt$Es=="NLD_2002"]
dt$Ll.3[dt$Es=="NLD_2002"]=dt$RSYML3[dt$Es=="NLD_2002"]
dt$Ll.4[dt$Es=="NLD_2002"]=dt$RSYML1[dt$Es=="NLD_2002"]
dt$Ll.5[dt$Es=="NLD_2002"]=dt$RSYML5[dt$Es=="NLD_2002"]
dt$Ll.6[dt$Es=="NLD_2002"]=dt$RSYML7[dt$Es=="NLD_2002"]

dt$Ll.1[dt$Es=="NOR_2001"]=dt$RSYML2[dt$Es=="NOR_2001"]
dt$Ll.2[dt$Es=="NOR_2001"]=dt$RSYML6[dt$Es=="NOR_2001"]
dt$Ll.3[dt$Es=="NOR_2001"]=dt$RSYML7[dt$Es=="NOR_2001"]
dt$Ll.4[dt$Es=="NOR_2001"]=dt$RSYML1[dt$Es=="NOR_2001"]
dt$Ll.5[dt$Es=="NOR_2001"]=dt$RSYML4[dt$Es=="NOR_2001"]
dt$Ll.6[dt$Es=="NOR_2001"]=dt$RSYML5[dt$Es=="NOR_2001"]

dt$Ll.1[dt$Es=="PRT_2005"]=dt$RSYML5[dt$Es=="PRT_2005"]
dt$Ll.2[dt$Es=="PRT_2005"]=dt$RSYML4[dt$Es=="PRT_2005"]
dt$Ll.3[dt$Es=="PRT_2005"]=dt$RSYML3[dt$Es=="PRT_2005"]
dt$Ll.4[dt$Es=="PRT_2005"]=dt$RSYML2[dt$Es=="PRT_2005"]
dt$Ll.5[dt$Es=="PRT_2005"]=dt$RSYML1[dt$Es=="PRT_2005"]

dt$Ll.1[dt$Es=="SVN_2000"]=dt$RSYML2[dt$Es=="SVN_2000"]
dt$Ll.2[dt$Es=="SVN_2000"]=dt$RSYML5[dt$Es=="SVN_2000"]
dt$Ll.3[dt$Es=="SVN_2000"]=dt$RSYML7[dt$Es=="SVN_2000"]
dt$Ll.4[dt$Es=="SVN_2000"]=dt$RSYML3[dt$Es=="SVN_2000"]
dt$Ll.5[dt$Es=="SVN_2000"]=dt$RSYML6[dt$Es=="SVN_2000"]
dt$Ll.6[dt$Es=="SVN_2000"]=dt$RSYML1[dt$Es=="SVN_2000"]

dt$Ll.1[dt$Es=="SWE_1979"]=dt$RSYML2[dt$Es=="SWE_1979"]
dt$Ll.2[dt$Es=="SWE_1979"]=dt$RSYML5[dt$Es=="SWE_1979"]
dt$Ll.3[dt$Es=="SWE_1979"]=dt$RSYML3[dt$Es=="SWE_1979"]
dt$Ll.4[dt$Es=="SWE_1979"]=dt$RSYML4[dt$Es=="SWE_1979"]
dt$Ll.5[dt$Es=="SWE_1979"]=dt$RSYML1[dt$Es=="SWE_1979"]

dt$Ll.1[dt$Es=="SWE_2002"]=dt$RSYML2[dt$Es=="SWE_2002"]
dt$Ll.2[dt$Es=="SWE_2002"]=dt$RSYML5[dt$Es=="SWE_2002"]
dt$Ll.3[dt$Es=="SWE_2002"]=dt$RSYML4[dt$Es=="SWE_2002"]
dt$Ll.4[dt$Es=="SWE_2002"]=dt$RSYML6[dt$Es=="SWE_2002"]
dt$Ll.5[dt$Es=="SWE_2002"]=dt$RSYML1[dt$Es=="SWE_2002"]
dt$Ll.6[dt$Es=="SWE_2002"]=dt$RSYML3[dt$Es=="SWE_2002"]

dt$Ll.1[dt$Es=="SWE_2010"]=dt$RSYML2[dt$Es=="SWE_2010"]
dt$Ll.2[dt$Es=="SWE_2010"]=dt$RSYML5[dt$Es=="SWE_2010"]
dt$Ll.3[dt$Es=="SWE_2010"]=dt$RSYML7[dt$Es=="SWE_2010"]
dt$Ll.4[dt$Es=="SWE_2010"]=dt$RSYML4[dt$Es=="SWE_2010"]
dt$Ll.5[dt$Es=="SWE_2010"]=dt$RSYML3[dt$Es=="SWE_2010"]
dt$Ll.6[dt$Es=="SWE_2010"]=dt$RSYML10[dt$Es=="SWE_2010"]

#common elections TEV-CSES (minus exclusions, see "check common elections"):
dt$Ll.1[dt$Es=="DEU_1961"]=dt$RSYML2[dt$Es=="DEU_1961"]
dt$Ll.2[dt$Es=="DEU_1961"]=dt$RSYML1[dt$Es=="DEU_1961"]
dt$Ll.3[dt$Es=="DEU_1961"]=dt$RSYML3[dt$Es=="DEU_1961"]

dt$Ll.1[dt$Es=="DEU_1965"]=dt$RSYML2[dt$Es=="DEU_1965"]
dt$Ll.2[dt$Es=="DEU_1965"]=dt$RSYML1[dt$Es=="DEU_1965"]
dt$Ll.3[dt$Es=="DEU_1965"]=dt$RSYML3[dt$Es=="DEU_1965"]

dt$Ll.1[dt$Es=="DEU_1976"]=dt$RSYML2[dt$Es=="DEU_1976"]
dt$Ll.2[dt$Es=="DEU_1976"]=dt$RSYML1[dt$Es=="DEU_1976"]
dt$Ll.3[dt$Es=="DEU_1976"]=dt$RSYML3[dt$Es=="DEU_1976"]

dt$Ll.1[dt$Es=="DEU_1980"]=dt$RSYML2[dt$Es=="DEU_1980"]
dt$Ll.2[dt$Es=="DEU_1980"]=dt$RSYML1[dt$Es=="DEU_1980"]
dt$Ll.3[dt$Es=="DEU_1980"]=dt$RSYML3[dt$Es=="DEU_1980"]

dt$Ll.1[dt$Es=="DEU_1983"]=dt$RSYML2[dt$Es=="DEU_1983"]
dt$Ll.2[dt$Es=="DEU_1983"]=dt$RSYML1[dt$Es=="DEU_1983"]
dt$Ll.3[dt$Es=="DEU_1983"]=dt$RSYML3[dt$Es=="DEU_1983"]

dt$Ll.1[dt$Es=="DEU_1987"]=dt$RSYML2[dt$Es=="DEU_1987"]
dt$Ll.2[dt$Es=="DEU_1987"]=dt$RSYML1[dt$Es=="DEU_1987"]
dt$Ll.3[dt$Es=="DEU_1987"]=dt$RSYML3[dt$Es=="DEU_1987"]

dt$Ll.1[dt$Es=="DEU_1990"]=dt$RSYML2[dt$Es=="DEU_1990"]
dt$Ll.2[dt$Es=="DEU_1990"]=dt$RSYML1[dt$Es=="DEU_1990"]
dt$Ll.3[dt$Es=="DEU_1990"]=dt$RSYML3[dt$Es=="DEU_1990"]
dt$Ll.4[dt$Es=="DEU_1990"]=dt$RSYML5[dt$Es=="DEU_1990"]

dt$Ll.1[dt$Es=="DEU_1994"]=dt$RSYML2[dt$Es=="DEU_1994"]
dt$Ll.2[dt$Es=="DEU_1994"]=dt$RSYML1[dt$Es=="DEU_1994"]
dt$Ll.3[dt$Es=="DEU_1994"]=dt$RSYML4[dt$Es=="DEU_1994"]
dt$Ll.4[dt$Es=="DEU_1994"]=dt$RSYML3[dt$Es=="DEU_1994"]
dt$Ll.5[dt$Es=="DEU_1994"]=dt$RSYML5[dt$Es=="DEU_1994"]

dt$Ll.1[dt$Es=="DEU_1998"]=dt$RSYML1[dt$Es=="DEU_1998"]
dt$Ll.2[dt$Es=="DEU_1998"]=dt$RSYML2[dt$Es=="DEU_1998"]
dt$Ll.3[dt$Es=="DEU_1998"]=dt$RSYML4[dt$Es=="DEU_1998"]
dt$Ll.4[dt$Es=="DEU_1998"]=dt$RSYML3[dt$Es=="DEU_1998"]
dt$Ll.5[dt$Es=="DEU_1998"]=dt$RSYML5[dt$Es=="DEU_1998"]

dt$Ll.1[dt$Es=="DEU_2009"]=dt$RSYML2[dt$Es=="DEU_2009"]
dt$Ll.2[dt$Es=="DEU_2009"]=dt$RSYML1[dt$Es=="DEU_2009"]
dt$Ll.3[dt$Es=="DEU_2009"]=dt$RSYML3[dt$Es=="DEU_2009"]
dt$Ll.4[dt$Es=="DEU_2009"]=dt$RSYML5[dt$Es=="DEU_2009"]
dt$Ll.5[dt$Es=="DEU_2009"]=dt$RSYML4[dt$Es=="DEU_2009"]

dt$Ll.1[dt$Es=="DEU_2013"]=dt$RSYML2[dt$Es=="DEU_2013"]
dt$Ll.2[dt$Es=="DEU_2013"]=dt$RSYML1[dt$Es=="DEU_2013"]
dt$Ll.3[dt$Es=="DEU_2013"]=dt$RSYML5[dt$Es=="DEU_2013"]
dt$Ll.4[dt$Es=="DEU_2013"]=dt$RSYML4[dt$Es=="DEU_2013"]
dt$Ll.5[dt$Es=="DEU_2013"]=dt$RSYML3[dt$Es=="DEU_2013"]

dt$Ll.1[dt$Es=="FIN_2007"]=dt$RSYML1[dt$Es=="FIN_2007"]
dt$Ll.2[dt$Es=="FIN_2007"]=dt$RSYML3[dt$Es=="FIN_2007"]
dt$Ll.3[dt$Es=="FIN_2007"]=dt$RSYML2[dt$Es=="FIN_2007"]
dt$Ll.4[dt$Es=="FIN_2007"]=dt$RSYML4[dt$Es=="FIN_2007"]
dt$Ll.5[dt$Es=="FIN_2007"]=dt$RSYML5[dt$Es=="FIN_2007"]
dt$Ll.6[dt$Es=="FIN_2007"]=dt$RSYML7[dt$Es=="FIN_2007"]

dt$Ll.1[dt$Es=="FIN_2011"]=dt$RSYML3[dt$Es=="FIN_2011"]
dt$Ll.2[dt$Es=="FIN_2011"]=dt$RSYML2[dt$Es=="FIN_2011"]
dt$Ll.3[dt$Es=="FIN_2011"]=dt$RSYML8[dt$Es=="FIN_2011"]
dt$Ll.4[dt$Es=="FIN_2011"]=dt$RSYML1[dt$Es=="FIN_2011"]
dt$Ll.5[dt$Es=="FIN_2011"]=dt$RSYML4[dt$Es=="FIN_2011"]
dt$Ll.6[dt$Es=="FIN_2011"]=dt$RSYML5[dt$Es=="FIN_2011"]

dt$Ll.1[dt$Es=="GRC_2009"]=dt$RSYML1[dt$Es=="GRC_2009"]
dt$Ll.2[dt$Es=="GRC_2009"]=dt$RSYML2[dt$Es=="GRC_2009"]
dt$Ll.3[dt$Es=="GRC_2009"]=dt$RSYML3[dt$Es=="GRC_2009"]
dt$Ll.4[dt$Es=="GRC_2009"]=dt$RSYML4[dt$Es=="GRC_2009"]
dt$Ll.5[dt$Es=="GRC_2009"]=dt$RSYML5[dt$Es=="GRC_2009"]
dt$Ll.6[dt$Es=="GRC_2009"]=dt$RSYML6[dt$Es=="GRC_2009"]

dt$Ll.1[dt$Es=="GRC_2012"]=dt$RSYML1[dt$Es=="GRC_2012"]
dt$Ll.2[dt$Es=="GRC_2012"]=dt$RSYML2[dt$Es=="GRC_2012"]
dt$Ll.3[dt$Es=="GRC_2012"]=dt$RSYML3[dt$Es=="GRC_2012"]
dt$Ll.4[dt$Es=="GRC_2012"]=dt$RSYML4[dt$Es=="GRC_2012"]
dt$Ll.5[dt$Es=="GRC_2012"]=dt$RSYML5[dt$Es=="GRC_2012"]
dt$Ll.6[dt$Es=="GRC_2012"]=dt$RSYML6[dt$Es=="GRC_2012"]

dt$Ll.1[dt$Es=="IRL_2007"]=dt$RSYML1[dt$Es=="IRL_2007"]
dt$Ll.2[dt$Es=="IRL_2007"]=dt$RSYML2[dt$Es=="IRL_2007"]
dt$Ll.3[dt$Es=="IRL_2007"]=dt$RSYML4[dt$Es=="IRL_2007"]
dt$Ll.4[dt$Es=="IRL_2007"]=dt$RSYML3[dt$Es=="IRL_2007"]
dt$Ll.5[dt$Es=="IRL_2007"]=dt$RSYML6[dt$Es=="IRL_2007"]
dt$Ll.6[dt$Es=="IRL_2007"]=dt$RSYML5[dt$Es=="IRL_2007"]

dt$Ll.1[dt$Es=="IRL_2011"]=dt$RSYML2[dt$Es=="IRL_2011"]
dt$Ll.2[dt$Es=="IRL_2011"]=dt$RSYML3[dt$Es=="IRL_2011"]
dt$Ll.3[dt$Es=="IRL_2011"]=dt$RSYML1[dt$Es=="IRL_2011"]
dt$Ll.4[dt$Es=="IRL_2011"]=dt$RSYML4[dt$Es=="IRL_2011"]
dt$Ll.5[dt$Es=="IRL_2011"]=dt$RSYML5[dt$Es=="IRL_2011"]#

dt$Ll.1[dt$Es=="ISL_1999"]=dt$RSYML3[dt$Es=="ISL_1999"]
dt$Ll.2[dt$Es=="ISL_1999"]=dt$RSYML1[dt$Es=="ISL_1999"]
dt$Ll.3[dt$Es=="ISL_1999"]=dt$RSYML2[dt$Es=="ISL_1999"]
dt$Ll.4[dt$Es=="ISL_1999"]=dt$RSYML4[dt$Es=="ISL_1999"]
dt$Ll.5[dt$Es=="ISL_1999"]=dt$RSYML5[dt$Es=="ISL_1999"]

dt$Ll.1[dt$Es=="ISL_2007"]=dt$RSYML3[dt$Es=="ISL_2007"]
dt$Ll.2[dt$Es=="ISL_2007"]=dt$RSYML1[dt$Es=="ISL_2007"]
dt$Ll.3[dt$Es=="ISL_2007"]=dt$RSYML4[dt$Es=="ISL_2007"]
dt$Ll.4[dt$Es=="ISL_2007"]=dt$RSYML2[dt$Es=="ISL_2007"]
dt$Ll.5[dt$Es=="ISL_2007"]=dt$RSYML5[dt$Es=="ISL_2007"]
dt$Ll.6[dt$Es=="ISL_2007"]=dt$RSYML6[dt$Es=="ISL_2007"]#

dt$Ll.1[dt$Es=="ISL_2009"]=dt$RSYML1[dt$Es=="ISL_2009"]
dt$Ll.2[dt$Es=="ISL_2009"]=dt$RSYML3[dt$Es=="ISL_2009"]
dt$Ll.3[dt$Es=="ISL_2009"]=dt$RSYML4[dt$Es=="ISL_2009"]
dt$Ll.4[dt$Es=="ISL_2009"]=dt$RSYML2[dt$Es=="ISL_2009"]
dt$Ll.5[dt$Es=="ISL_2009"]=dt$RSYML5[dt$Es=="ISL_2009"]#
dt$Ll.6[dt$Es=="ISL_2009"]=dt$RSYML6[dt$Es=="ISL_2009"]#

dt$Ll.1[dt$Es=="ISL_2013"]=dt$RSYML3[dt$Es=="ISL_2013"]
dt$Ll.2[dt$Es=="ISL_2013"]=dt$RSYML2[dt$Es=="ISL_2013"]
dt$Ll.3[dt$Es=="ISL_2013"]=dt$RSYML1[dt$Es=="ISL_2013"]
dt$Ll.4[dt$Es=="ISL_2013"]=dt$RSYML4[dt$Es=="ISL_2013"]
dt$Ll.5[dt$Es=="ISL_2013"]=dt$RSYML5[dt$Es=="ISL_2013"]
dt$Ll.6[dt$Es=="ISL_2013"]=dt$RSYML6[dt$Es=="ISL_2013"]

dt$Ll.1[dt$Es=="NLD_1994"]=dt$RSYML1[dt$Es=="NLD_1994"]
dt$Ll.2[dt$Es=="NLD_1994"]=dt$RSYML2[dt$Es=="NLD_1994"]
dt$Ll.3[dt$Es=="NLD_1994"]=dt$RSYML3[dt$Es=="NLD_1994"]
dt$Ll.4[dt$Es=="NLD_1994"]=dt$RSYML4[dt$Es=="NLD_1994"]

dt$Ll.1[dt$Es=="NLD_1998"]=dt$RSYML1[dt$Es=="NLD_1998"]#EV 4 parties, CSES 6 parties coded
dt$Ll.2[dt$Es=="NLD_1998"]=dt$RSYML3[dt$Es=="NLD_1998"]
dt$Ll.3[dt$Es=="NLD_1998"]=dt$RSYML2[dt$Es=="NLD_1998"]
dt$Ll.4[dt$Es=="NLD_1998"]=dt$RSYML4[dt$Es=="NLD_1998"]
dt$Ll.5[dt$Es=="NLD_1998"]=dt$RSYML5[dt$Es=="NLD_1998"]
dt$Ll.6[dt$Es=="NLD_1998"]=dt$RSYML7[dt$Es=="NLD_1998"]#

dt$Ll.1[dt$Es=="NOR_1981"]=dt$RSYML2[dt$Es=="NOR_1981"]
dt$Ll.2[dt$Es=="NOR_1981"]=dt$RSYML6[dt$Es=="NOR_1981"]
dt$Ll.3[dt$Es=="NOR_1981"]=dt$RSYML4[dt$Es=="NOR_1981"]
dt$Ll.4[dt$Es=="NOR_1981"]=dt$RSYML5[dt$Es=="NOR_1981"]
dt$Ll.5[dt$Es=="NOR_1981"]=dt$RSYML1[dt$Es=="NOR_1981"]
dt$Ll.6[dt$Es=="NOR_1981"]=dt$RSYML7[dt$Es=="NOR_1981"]

dt$Ll.1[dt$Es=="NOR_1985"]=dt$RSYML2[dt$Es=="NOR_1985"]
dt$Ll.2[dt$Es=="NOR_1985"]=dt$RSYML6[dt$Es=="NOR_1985"]
dt$Ll.3[dt$Es=="NOR_1985"]=dt$RSYML4[dt$Es=="NOR_1985"]
dt$Ll.4[dt$Es=="NOR_1985"]=dt$RSYML5[dt$Es=="NOR_1985"]
dt$Ll.5[dt$Es=="NOR_1985"]=dt$RSYML1[dt$Es=="NOR_1985"]
dt$Ll.6[dt$Es=="NOR_1985"]=dt$RSYML7[dt$Es=="NOR_1985"]

dt$Ll.1[dt$Es=="NOR_1989"]=dt$RSYML2[dt$Es=="NOR_1989"]
dt$Ll.2[dt$Es=="NOR_1989"]=dt$RSYML6[dt$Es=="NOR_1989"]
dt$Ll.3[dt$Es=="NOR_1989"]=dt$RSYML7[dt$Es=="NOR_1989"]
dt$Ll.4[dt$Es=="NOR_1989"]=dt$RSYML1[dt$Es=="NOR_1989"]
dt$Ll.5[dt$Es=="NOR_1989"]=dt$RSYML4[dt$Es=="NOR_1989"]
dt$Ll.6[dt$Es=="NOR_1989"]=dt$RSYML5[dt$Es=="NOR_1989"]

dt$Ll.1[dt$Es=="NOR_1993"]=dt$RSYML2[dt$Es=="NOR_1993"]
dt$Ll.2[dt$Es=="NOR_1993"]=dt$RSYML5[dt$Es=="NOR_1993"]
dt$Ll.3[dt$Es=="NOR_1993"]=dt$RSYML6[dt$Es=="NOR_1993"]
dt$Ll.4[dt$Es=="NOR_1993"]=dt$RSYML1[dt$Es=="NOR_1993"]
dt$Ll.5[dt$Es=="NOR_1993"]=dt$RSYML4[dt$Es=="NOR_1993"]
dt$Ll.6[dt$Es=="NOR_1993"]=dt$RSYML7[dt$Es=="NOR_1993"]

dt$Ll.1[dt$Es=="NOR_1997"]=dt$RSYML2[dt$Es=="NOR_1997"]
dt$Ll.2[dt$Es=="NOR_1997"]=dt$RSYML7[dt$Es=="NOR_1997"]
dt$Ll.3[dt$Es=="NOR_1997"]=dt$RSYML4[dt$Es=="NOR_1997"]
dt$Ll.4[dt$Es=="NOR_1997"]=dt$RSYML6[dt$Es=="NOR_1997"]
dt$Ll.5[dt$Es=="NOR_1997"]=dt$RSYML5[dt$Es=="NOR_1997"]
dt$Ll.6[dt$Es=="NOR_1997"]=dt$RSYML1[dt$Es=="NOR_1997"]

dt$Ll.1[dt$Es=="NOR_2005"]=dt$RSYML2[dt$Es=="NOR_2005"]
dt$Ll.2[dt$Es=="NOR_2005"]=dt$RSYML7[dt$Es=="NOR_2005"]
dt$Ll.3[dt$Es=="NOR_2005"]=dt$RSYML6[dt$Es=="NOR_2005"]
dt$Ll.4[dt$Es=="NOR_2005"]=dt$RSYML1[dt$Es=="NOR_2005"]
dt$Ll.5[dt$Es=="NOR_2005"]=dt$RSYML4[dt$Es=="NOR_2005"]
dt$Ll.6[dt$Es=="NOR_2005"]=dt$RSYML5[dt$Es=="NOR_2005"]

dt$Ll.1[dt$Es=="PRT_2002"]=dt$RSYML4[dt$Es=="PRT_2002"]
dt$Ll.2[dt$Es=="PRT_2002"]=dt$RSYML5[dt$Es=="PRT_2002"]
dt$Ll.3[dt$Es=="PRT_2002"]=dt$RSYML2[dt$Es=="PRT_2002"]
dt$Ll.4[dt$Es=="PRT_2002"]=dt$RSYML3[dt$Es=="PRT_2002"]
dt$Ll.5[dt$Es=="PRT_2002"]=dt$RSYML1[dt$Es=="PRT_2002"]
dt$Ll.6[dt$Es=="PRT_2002"]=dt$RSYML6[dt$Es=="PRT_2002"]

dt$Ll.1[dt$Es=="PRT_2009"]=dt$RSYML5[dt$Es=="PRT_2009"]#osym4-5  inverted!?
dt$Ll.2[dt$Es=="PRT_2009"]=dt$RSYML4[dt$Es=="PRT_2009"]
dt$Ll.3[dt$Es=="PRT_2009"]=dt$RSYML2[dt$Es=="PRT_2009"]
dt$Ll.4[dt$Es=="PRT_2009"]=dt$RSYML1[dt$Es=="PRT_2009"]
dt$Ll.5[dt$Es=="PRT_2009"]=dt$RSYML3[dt$Es=="PRT_2009"]

dt$Ll.1[dt$Es=="SWE_1982"]=dt$RSYML2[dt$Es=="SWE_1982"]
dt$Ll.2[dt$Es=="SWE_1982"]=dt$RSYML5[dt$Es=="SWE_1982"]
dt$Ll.3[dt$Es=="SWE_1982"]=dt$RSYML3[dt$Es=="SWE_1982"]
dt$Ll.4[dt$Es=="SWE_1982"]=dt$RSYML4[dt$Es=="SWE_1982"]
dt$Ll.5[dt$Es=="SWE_1982"]=dt$RSYML1[dt$Es=="SWE_1982"]

dt$Ll.1[dt$Es=="SWE_1985"]=dt$RSYML2[dt$Es=="SWE_1985"]
dt$Ll.2[dt$Es=="SWE_1985"]=dt$RSYML5[dt$Es=="SWE_1985"]
dt$Ll.3[dt$Es=="SWE_1985"]=dt$RSYML4[dt$Es=="SWE_1985"]
dt$Ll.4[dt$Es=="SWE_1985"]=dt$RSYML3[dt$Es=="SWE_1985"]
dt$Ll.5[dt$Es=="SWE_1985"]=dt$RSYML1[dt$Es=="SWE_1985"]
dt$Ll.6[dt$Es=="SWE_1985"]=dt$RSYML6[dt$Es=="SWE_1985"]

dt$Ll.1[dt$Es=="SWE_1988"]=dt$RSYML2[dt$Es=="SWE_1988"]
dt$Ll.2[dt$Es=="SWE_1988"]=dt$RSYML5[dt$Es=="SWE_1988"]
dt$Ll.3[dt$Es=="SWE_1988"]=dt$RSYML4[dt$Es=="SWE_1988"]
dt$Ll.4[dt$Es=="SWE_1988"]=dt$RSYML3[dt$Es=="SWE_1988"]
dt$Ll.5[dt$Es=="SWE_1988"]=dt$RSYML1[dt$Es=="SWE_1988"]

dt$Ll.1[dt$Es=="SWE_1991"]=dt$RSYML2[dt$Es=="SWE_1991"]
dt$Ll.2[dt$Es=="SWE_1991"]=dt$RSYML5[dt$Es=="SWE_1991"]
dt$Ll.3[dt$Es=="SWE_1991"]=dt$RSYML4[dt$Es=="SWE_1991"]
dt$Ll.4[dt$Es=="SWE_1991"]=dt$RSYML3[dt$Es=="SWE_1991"]
dt$Ll.5[dt$Es=="SWE_1991"]=dt$RSYML6[dt$Es=="SWE_1991"]
dt$Ll.6[dt$Es=="SWE_1991"]=dt$RSYML9[dt$Es=="SWE_1991"]

dt$Ll.1[dt$Es=="SWE_1994"]=dt$RSYML2[dt$Es=="SWE_1994"]
dt$Ll.2[dt$Es=="SWE_1994"]=dt$RSYML5[dt$Es=="SWE_1994"]
dt$Ll.3[dt$Es=="SWE_1994"]=dt$RSYML3[dt$Es=="SWE_1994"]
dt$Ll.4[dt$Es=="SWE_1994"]=dt$RSYML4[dt$Es=="SWE_1994"]
dt$Ll.5[dt$Es=="SWE_1994"]=dt$RSYML1[dt$Es=="SWE_1994"]
dt$Ll.6[dt$Es=="SWE_1994"]=dt$RSYML7[dt$Es=="SWE_1994"]

dt$Ll.1[dt$Es=="SWE_1998"]=dt$RSYML2[dt$Es=="SWE_1998"]
dt$Ll.2[dt$Es=="SWE_1998"]=dt$RSYML5[dt$Es=="SWE_1998"]
dt$Ll.3[dt$Es=="SWE_1998"]=dt$RSYML1[dt$Es=="SWE_1998"]
dt$Ll.4[dt$Es=="SWE_1998"]=dt$RSYML6[dt$Es=="SWE_1998"]
dt$Ll.5[dt$Es=="SWE_1998"]=dt$RSYML3[dt$Es=="SWE_1998"]
dt$Ll.6[dt$Es=="SWE_1998"]=dt$RSYML4[dt$Es=="SWE_1998"]

dt$Ll.1[dt$Es=="SWE_2006"]=dt$RSYML2[dt$Es=="SWE_2006"]
dt$Ll.2[dt$Es=="SWE_2006"]=dt$RSYML5[dt$Es=="SWE_2006"]
dt$Ll.3[dt$Es=="SWE_2006"]=dt$RSYML3[dt$Es=="SWE_2006"]
dt$Ll.4[dt$Es=="SWE_2006"]=dt$RSYML4[dt$Es=="SWE_2006"]
dt$Ll.5[dt$Es=="SWE_2006"]=dt$RSYML6[dt$Es=="SWE_2006"]
dt$Ll.6[dt$Es=="SWE_2006"]=dt$RSYML1[dt$Es=="SWE_2006"]


dt$Ll.1=as.numeric(dt$Ll.1)#recode as numeric:
dt$Ll.2=as.numeric(dt$Ll.2)
dt$Ll.3=as.numeric(dt$Ll.3)
dt$Ll.4=as.numeric(dt$Ll.4)
dt$Ll.5=as.numeric(dt$Ll.5)
dt$Ll.6=as.numeric(dt$Ll.6)

dt$Ll.1[dt$Ll.1>1]=NA#exclude NAs coded 900s:
dt$Ll.2[dt$Ll.2>1]=NA
dt$Ll.3[dt$Ll.3>1]=NA
dt$Ll.4[dt$Ll.4>1]=NA
dt$Ll.5[dt$Ll.5>1]=NA
dt$Ll.6[dt$Ll.6>1]=NA

dt$Ll.1=dt$Ll.1*10#recode to 0-10 scale:
dt$Ll.2=dt$Ll.2*10
dt$Ll.3=dt$Ll.3*10
dt$Ll.4=dt$Ll.4*10
dt$Ll.5=dt$Ll.5*10
dt$Ll.6=dt$Ll.6*10

#subset:----

#keep only respondents (and elections) with coded Pl,Ll:
# (I keep here elections with at least one party coded for either Pl or Ll, which is the same)
Pl=dt[, names(dt) %in% c(paste0("Pl.", 1:14))]#create matrix Pl data
Pl=as.matrix(Pl)
dt=dt[dt$Es %in% c(unique(dt$Es[rowMins(Pl, na.rm=T)<20])),]#keep only Es with Pl for at least one party
Ll=dt[, names(dt) %in% c(paste0("Ll.", 1:14))]#create matrix Ll data
Ll=as.matrix(Ll)
dt=dt[dt$Es %in% c(unique(dt$Es[rowMins(Ll, na.rm=T)<20])),]#keep only Es with Ll for at least one party
rm(Pl,Ll)

#get rid of elections with less than three parties and/or leaders coded:
# (already excluded GRC_1989j)
dt=dt[!(dt$Es %in% c("DEU_1972","DEU_2002","GRC_1993","ITA_2013","PRT_1985")),]

#get rid of elections non free Freedom House:
dt=dt[!(dt$Es %in% c("TUR_2007","TUR_2011")),]

#exclude presidential systems:
dew=dew[!(dew$Ec %in% c("FRA","MEX","PER")),]


#LRR----
colnames(dt)[colnames(dt)=="RLRSP"]="LRR"
dt$LRR=as.numeric(dt$LRR)
dt$LRR[dt$LRR>1]=NA
dt$LRR=dt$LRR*10


#LRPi:----
dt$LRP.1=NA#create LRP:
dt$LRP.2=NA
dt$LRP.3=NA
dt$LRP.4=NA
dt$LRP.5=NA
dt$LRP.6=NA

dt$LRP.1[dt$Es=="ESP_1979"]=dt$RLRPP1[dt$Es=="ESP_1979"]#assign LRP by party size order for parties with likability:
dt$LRP.2[dt$Es=="ESP_1979"]=dt$RLRPP2[dt$Es=="ESP_1979"]
dt$LRP.3[dt$Es=="ESP_1979"]=dt$RLRPP3[dt$Es=="ESP_1979"]
dt$LRP.4[dt$Es=="ESP_1979"]=dt$RLRPP4[dt$Es=="ESP_1979"]

dt$LRP.1[dt$Es=="ESP_1982"]=dt$RLRPP3[dt$Es=="ESP_1982"]
dt$LRP.2[dt$Es=="ESP_1982"]=dt$RLRPP2[dt$Es=="ESP_1982"]
dt$LRP.3[dt$Es=="ESP_1982"]=dt$RLRPP4[dt$Es=="ESP_1982"]
dt$LRP.4[dt$Es=="ESP_1982"]=dt$RLRPP1[dt$Es=="ESP_1982"]
dt$LRP.5[dt$Es=="ESP_1982"]=dt$RLRPP5[dt$Es=="ESP_1982"]

dt$LRP.1[dt$Es=="ESP_1993"]=dt$RLRPP1[dt$Es=="ESP_1993"]
dt$LRP.2[dt$Es=="ESP_1993"]=dt$RLRPP3[dt$Es=="ESP_1993"]
dt$LRP.3[dt$Es=="ESP_1993"]=dt$RLRPP4[dt$Es=="ESP_1993"]
dt$LRP.4[dt$Es=="ESP_1993"]=dt$RLRPP2[dt$Es=="ESP_1993"]

dt$LRP.1[dt$Es=="ESP_2004"]=dt$RLRPP1[dt$Es=="ESP_2004"]
dt$LRP.2[dt$Es=="ESP_2004"]=dt$RLRPP2[dt$Es=="ESP_2004"]
dt$LRP.3[dt$Es=="ESP_2004"]=dt$RLRPP3[dt$Es=="ESP_2004"]

dt$LRP.1[dt$Es=="EST_2011"]=dt$RLRPP1[dt$Es=="EST_2011"]
dt$LRP.2[dt$Es=="EST_2011"]=dt$RLRPP2[dt$Es=="EST_2011"]
dt$LRP.3[dt$Es=="EST_2011"]=dt$RLRPP3[dt$Es=="EST_2011"]
dt$LRP.4[dt$Es=="EST_2011"]=dt$RLRPP4[dt$Es=="EST_2011"]
dt$LRP.5[dt$Es=="EST_2011"]=dt$RLRPP6[dt$Es=="EST_2011"]
dt$LRP.6[dt$Es=="EST_2011"]=dt$RLRPP5[dt$Es=="EST_2011"]

dt$LRP.1[dt$Es=="FIN_2003"]=dt$RLRPP1[dt$Es=="FIN_2003"]
dt$LRP.2[dt$Es=="FIN_2003"]=dt$RLRPP3[dt$Es=="FIN_2003"]
dt$LRP.3[dt$Es=="FIN_2003"]=dt$RLRPP2[dt$Es=="FIN_2003"]
dt$LRP.4[dt$Es=="FIN_2003"]=dt$RLRPP4[dt$Es=="FIN_2003"]
dt$LRP.5[dt$Es=="FIN_2003"]=dt$RLRPP5[dt$Es=="FIN_2003"]
dt$LRP.6[dt$Es=="FIN_2003"]=dt$RLRPP6[dt$Es=="FIN_2003"]

dt$LRP.1[dt$Es=="GBR_2005"]=dt$RLRPP1[dt$Es=="GBR_2005"]
dt$LRP.2[dt$Es=="GBR_2005"]=dt$RLRPP2[dt$Es=="GBR_2005"]
dt$LRP.3[dt$Es=="GBR_2005"]=dt$RLRPP3[dt$Es=="GBR_2005"]

dt$LRP.1[dt$Es=="GRC_1985"]=dt$RLRPP1[dt$Es=="GRC_1985"]
dt$LRP.2[dt$Es=="GRC_1985"]=dt$RLRPP2[dt$Es=="GRC_1985"]
dt$LRP.3[dt$Es=="GRC_1985"]=dt$RLRPP3[dt$Es=="GRC_1985"]
dt$LRP.4[dt$Es=="GRC_1985"]=dt$RLRPP4[dt$Es=="GRC_1985"]

dt$LRP.1[dt$Es=="GRC_1989"]=dt$RLRPP1[dt$Es=="GRC_1989"]
dt$LRP.2[dt$Es=="GRC_1989"]=dt$RLRPP2[dt$Es=="GRC_1989"]
dt$LRP.3[dt$Es=="GRC_1989"]=dt$RLRPP3[dt$Es=="GRC_1989"]

dt$LRP.1[dt$Es=="GRC_1990"]=dt$RLRPP1[dt$Es=="GRC_1990"]
dt$LRP.2[dt$Es=="GRC_1990"]=dt$RLRPP2[dt$Es=="GRC_1990"]
dt$LRP.3[dt$Es=="GRC_1990"]=dt$RLRPP3[dt$Es=="GRC_1990"]

dt$LRP.1[dt$Es=="GRC_1996"]=dt$RLRPP1[dt$Es=="GRC_1996"]
dt$LRP.2[dt$Es=="GRC_1996"]=dt$RLRPP2[dt$Es=="GRC_1996"]
dt$LRP.3[dt$Es=="GRC_1996"]=dt$RLRPP3[dt$Es=="GRC_1996"]
dt$LRP.4[dt$Es=="GRC_1996"]=dt$RLRPP4[dt$Es=="GRC_1996"]
dt$LRP.5[dt$Es=="GRC_1996"]=dt$RLRPP5[dt$Es=="GRC_1996"]

dt$LRP.1[dt$Es=="GRC_2000"]=dt$RLRPP1[dt$Es=="GRC_2000"]
dt$LRP.2[dt$Es=="GRC_2000"]=dt$RLRPP2[dt$Es=="GRC_2000"]
dt$LRP.3[dt$Es=="GRC_2000"]=dt$RLRPP3[dt$Es=="GRC_2000"]
dt$LRP.4[dt$Es=="GRC_2000"]=dt$RLRPP4[dt$Es=="GRC_2000"]

dt$LRP.1[dt$Es=="GRC_2004"]=dt$RLRPP1[dt$Es=="GRC_2004"]
dt$LRP.2[dt$Es=="GRC_2004"]=dt$RLRPP2[dt$Es=="GRC_2004"]
dt$LRP.3[dt$Es=="GRC_2004"]=dt$RLRPP3[dt$Es=="GRC_2004"]
dt$LRP.4[dt$Es=="GRC_2004"]=dt$RLRPP4[dt$Es=="GRC_2004"]

dt$LRP.1[dt$Es=="GRC_2007"]=dt$RLRPP1[dt$Es=="GRC_2007"]
dt$LRP.2[dt$Es=="GRC_2007"]=dt$RLRPP2[dt$Es=="GRC_2007"]
dt$LRP.3[dt$Es=="GRC_2007"]=dt$RLRPP3[dt$Es=="GRC_2007"]
dt$LRP.4[dt$Es=="GRC_2007"]=dt$RLRPP4[dt$Es=="GRC_2007"]
dt$LRP.5[dt$Es=="GRC_2007"]=dt$RLRPP5[dt$Es=="GRC_2007"]

dt$LRP.1[dt$Es=="HUN_1998"]=dt$RLRPP1[dt$Es=="HUN_1998"]
dt$LRP.2[dt$Es=="HUN_1998"]=dt$RLRPP5[dt$Es=="HUN_1998"]
dt$LRP.3[dt$Es=="HUN_1998"]=dt$RLRPP2[dt$Es=="HUN_1998"]
dt$LRP.4[dt$Es=="HUN_1998"]=dt$RLRPP6[dt$Es=="HUN_1998"]
dt$LRP.5[dt$Es=="HUN_1998"]=dt$RLRPP4[dt$Es=="HUN_1998"]
dt$LRP.6[dt$Es=="HUN_1998"]=dt$RLRPP3[dt$Es=="HUN_1998"]

dt$LRP.1[dt$Es=="HUN_2006"]=dt$RLRPP1[dt$Es=="HUN_2006"]
dt$LRP.2[dt$Es=="HUN_2006"]=dt$RLRPP2[dt$Es=="HUN_2006"]
dt$LRP.3[dt$Es=="HUN_2006"]=dt$RLRPP3[dt$Es=="HUN_2006"]
dt$LRP.4[dt$Es=="HUN_2006"]=dt$RLRPP4[dt$Es=="HUN_2006"]

dt$LRP.1[dt$Es=="IRL_2002"]=dt$RLRPP1[dt$Es=="IRL_2002"]
dt$LRP.2[dt$Es=="IRL_2002"]=dt$RLRPP2[dt$Es=="IRL_2002"]
dt$LRP.3[dt$Es=="IRL_2002"]=dt$RLRPP4[dt$Es=="IRL_2002"]
dt$LRP.4[dt$Es=="IRL_2002"]=dt$RLRPP5[dt$Es=="IRL_2002"]
dt$LRP.5[dt$Es=="IRL_2002"]=dt$RLRPP3[dt$Es=="IRL_2002"]
dt$LRP.6[dt$Es=="IRL_2002"]=dt$RLRPP6[dt$Es=="IRL_2002"]

dt$LRP.1[dt$Es=="ISL_1987"]=dt$RLRPP3[dt$Es=="ISL_1987"]
dt$LRP.2[dt$Es=="ISL_1987"]=dt$RLRPP2[dt$Es=="ISL_1987"]
dt$LRP.3[dt$Es=="ISL_1987"]=dt$RLRPP1[dt$Es=="ISL_1987"]
dt$LRP.4[dt$Es=="ISL_1987"]=dt$RLRPP4[dt$Es=="ISL_1987"]
dt$LRP.5[dt$Es=="ISL_1987"]=dt$RLRPP5[dt$Es=="ISL_1987"]

dt$LRP.1[dt$Es=="ISL_1991"]=dt$RLRPP3[dt$Es=="ISL_1991"]
dt$LRP.2[dt$Es=="ISL_1991"]=dt$RLRPP2[dt$Es=="ISL_1991"]
dt$LRP.3[dt$Es=="ISL_1991"]=dt$RLRPP1[dt$Es=="ISL_1991"]
dt$LRP.4[dt$Es=="ISL_1991"]=dt$RLRPP4[dt$Es=="ISL_1991"]
dt$LRP.5[dt$Es=="ISL_1991"]=dt$RLRPP5[dt$Es=="ISL_1991"]

dt$LRP.1[dt$Es=="ISL_1995"]=dt$RLRPP3[dt$Es=="ISL_1995"]
dt$LRP.2[dt$Es=="ISL_1995"]=dt$RLRPP2[dt$Es=="ISL_1995"]
dt$LRP.3[dt$Es=="ISL_1995"]=dt$RLRPP4[dt$Es=="ISL_1995"]
dt$LRP.4[dt$Es=="ISL_1995"]=dt$RLRPP1[dt$Es=="ISL_1995"]
dt$LRP.5[dt$Es=="ISL_1995"]=dt$RLRPP5[dt$Es=="ISL_1995"]
dt$LRP.6[dt$Es=="ISL_1995"]=dt$RLRPP6[dt$Es=="ISL_1995"]

dt$LRP.1[dt$Es=="ISL_2003"]=dt$RLRPP3[dt$Es=="ISL_2003"]
dt$LRP.2[dt$Es=="ISL_2003"]=dt$RLRPP1[dt$Es=="ISL_2003"]
dt$LRP.3[dt$Es=="ISL_2003"]=dt$RLRPP2[dt$Es=="ISL_2003"]
dt$LRP.4[dt$Es=="ISL_2003"]=dt$RLRPP4[dt$Es=="ISL_2003"]
dt$LRP.5[dt$Es=="ISL_2003"]=dt$RLRPP5[dt$Es=="ISL_2003"]

dt$LRP.1[dt$Es=="ITA_2006"]=dt$RLRPP3[dt$Es=="ITA_2006"]#coalition==1
dt$LRP.2[dt$Es=="ITA_2006"]=dt$RLRPP5[dt$Es=="ITA_2006"]
dt$LRP.3[dt$Es=="ITA_2006"]=dt$RLRPP6[dt$Es=="ITA_2006"]
dt$LRP.4[dt$Es=="ITA_2006"]=dt$RLRPP4[dt$Es=="ITA_2006"]
dt$LRP.5[dt$Es=="ITA_2006"]=dt$RLRPP1[dt$Es=="ITA_2006"]
dt$LRP.6[dt$Es=="ITA_2006"]=dt$RLRPP7[dt$Es=="ITA_2006"]

dt$LRP.1[dt$Es=="ITA_2008"]=dt$RLRPP4[dt$Es=="ITA_2008"]#coalition==1
dt$LRP.2[dt$Es=="ITA_2008"]=dt$RLRPP2[dt$Es=="ITA_2008"]
dt$LRP.3[dt$Es=="ITA_2008"]=dt$RLRPP5[dt$Es=="ITA_2008"]
dt$LRP.4[dt$Es=="ITA_2008"]=dt$RLRPP6[dt$Es=="ITA_2008"]
dt$LRP.5[dt$Es=="ITA_2008"]=dt$RLRPP3[dt$Es=="ITA_2008"]
dt$LRP.6[dt$Es=="ITA_2008"]=dt$RLRPP1[dt$Es=="ITA_2008"]

dt$LRP.1[dt$Es=="LTU_2000"]=dt$RLRPP2[dt$Es=="LTU_2000"]
dt$LRP.2[dt$Es=="LTU_2000"]=dt$RLRPP3[dt$Es=="LTU_2000"]
dt$LRP.3[dt$Es=="LTU_2000"]=dt$RLRPP4[dt$Es=="LTU_2000"]
dt$LRP.4[dt$Es=="LTU_2000"]=dt$RLRPP1[dt$Es=="LTU_2000"]

dt$LRP.1[dt$Es=="NLD_2002"]=dt$RLRPP2[dt$Es=="NLD_2002"]
dt$LRP.2[dt$Es=="NLD_2002"]=dt$RLRPP6[dt$Es=="NLD_2002"]
dt$LRP.3[dt$Es=="NLD_2002"]=dt$RLRPP3[dt$Es=="NLD_2002"]
dt$LRP.4[dt$Es=="NLD_2002"]=dt$RLRPP1[dt$Es=="NLD_2002"]
dt$LRP.5[dt$Es=="NLD_2002"]=dt$RLRPP5[dt$Es=="NLD_2002"]
dt$LRP.6[dt$Es=="NLD_2002"]=dt$RLRPP7[dt$Es=="NLD_2002"]

dt$LRP.1[dt$Es=="NOR_2001"]=dt$RLRPP2[dt$Es=="NOR_2001"]
dt$LRP.2[dt$Es=="NOR_2001"]=dt$RLRPP6[dt$Es=="NOR_2001"]
dt$LRP.3[dt$Es=="NOR_2001"]=dt$RLRPP7[dt$Es=="NOR_2001"]
dt$LRP.4[dt$Es=="NOR_2001"]=dt$RLRPP1[dt$Es=="NOR_2001"]
dt$LRP.5[dt$Es=="NOR_2001"]=dt$RLRPP4[dt$Es=="NOR_2001"]
dt$LRP.6[dt$Es=="NOR_2001"]=dt$RLRPP5[dt$Es=="NOR_2001"]

dt$LRP.1[dt$Es=="PRT_2005"]=dt$RLRPP5[dt$Es=="PRT_2005"]
dt$LRP.2[dt$Es=="PRT_2005"]=dt$RLRPP4[dt$Es=="PRT_2005"]
dt$LRP.3[dt$Es=="PRT_2005"]=dt$RLRPP3[dt$Es=="PRT_2005"]
dt$LRP.4[dt$Es=="PRT_2005"]=dt$RLRPP2[dt$Es=="PRT_2005"]
dt$LRP.5[dt$Es=="PRT_2005"]=dt$RLRPP1[dt$Es=="PRT_2005"]

dt$LRP.1[dt$Es=="SVN_2000"]=dt$RLRPP2[dt$Es=="SVN_2000"]
dt$LRP.2[dt$Es=="SVN_2000"]=dt$RLRPP5[dt$Es=="SVN_2000"]
dt$LRP.3[dt$Es=="SVN_2000"]=dt$RLRPP7[dt$Es=="SVN_2000"]
dt$LRP.4[dt$Es=="SVN_2000"]=dt$RLRPP3[dt$Es=="SVN_2000"]
dt$LRP.5[dt$Es=="SVN_2000"]=dt$RLRPP6[dt$Es=="SVN_2000"]
dt$LRP.6[dt$Es=="SVN_2000"]=dt$RLRPP1[dt$Es=="SVN_2000"]

dt$LRP.1[dt$Es=="SWE_1979"]=dt$RLRPP2[dt$Es=="SWE_1979"]
dt$LRP.2[dt$Es=="SWE_1979"]=dt$RLRPP5[dt$Es=="SWE_1979"]
dt$LRP.3[dt$Es=="SWE_1979"]=dt$RLRPP3[dt$Es=="SWE_1979"]
dt$LRP.4[dt$Es=="SWE_1979"]=dt$RLRPP4[dt$Es=="SWE_1979"]
dt$LRP.5[dt$Es=="SWE_1979"]=dt$RLRPP1[dt$Es=="SWE_1979"]

dt$LRP.1[dt$Es=="SWE_2002"]=dt$RLRPP2[dt$Es=="SWE_2002"]
dt$LRP.2[dt$Es=="SWE_2002"]=dt$RLRPP5[dt$Es=="SWE_2002"]
dt$LRP.3[dt$Es=="SWE_2002"]=dt$RLRPP4[dt$Es=="SWE_2002"]
dt$LRP.4[dt$Es=="SWE_2002"]=dt$RLRPP6[dt$Es=="SWE_2002"]
dt$LRP.5[dt$Es=="SWE_2002"]=dt$RLRPP1[dt$Es=="SWE_2002"]
dt$LRP.6[dt$Es=="SWE_2002"]=dt$RLRPP3[dt$Es=="SWE_2002"]

dt$LRP.1[dt$Es=="SWE_2010"]=dt$RLRPP2[dt$Es=="SWE_2010"]
dt$LRP.2[dt$Es=="SWE_2010"]=dt$RLRPP5[dt$Es=="SWE_2010"]
dt$LRP.3[dt$Es=="SWE_2010"]=dt$RLRPP7[dt$Es=="SWE_2010"]
dt$LRP.4[dt$Es=="SWE_2010"]=dt$RLRPP4[dt$Es=="SWE_2010"]
dt$LRP.5[dt$Es=="SWE_2010"]=dt$RLRPP3[dt$Es=="SWE_2010"]
dt$LRP.6[dt$Es=="SWE_2010"]=dt$RLRPP10[dt$Es=="SWE_2010"]

#common elections TEV-CSES (minus exclusions, see "check common elections"):
dt$LRP.1[dt$Es=="DEU_1961"]=dt$RLRPP2[dt$Es=="DEU_1961"]
dt$LRP.2[dt$Es=="DEU_1961"]=dt$RLRPP1[dt$Es=="DEU_1961"]
dt$LRP.3[dt$Es=="DEU_1961"]=dt$RLRPP3[dt$Es=="DEU_1961"]

dt$LRP.1[dt$Es=="DEU_1965"]=dt$RLRPP2[dt$Es=="DEU_1965"]
dt$LRP.2[dt$Es=="DEU_1965"]=dt$RLRPP1[dt$Es=="DEU_1965"]
dt$LRP.3[dt$Es=="DEU_1965"]=dt$RLRPP3[dt$Es=="DEU_1965"]

dt$LRP.1[dt$Es=="DEU_1976"]=dt$RLRPP2[dt$Es=="DEU_1976"]
dt$LRP.2[dt$Es=="DEU_1976"]=dt$RLRPP1[dt$Es=="DEU_1976"]
dt$LRP.3[dt$Es=="DEU_1976"]=dt$RLRPP3[dt$Es=="DEU_1976"]

dt$LRP.1[dt$Es=="DEU_1980"]=dt$RLRPP2[dt$Es=="DEU_1980"]
dt$LRP.2[dt$Es=="DEU_1980"]=dt$RLRPP1[dt$Es=="DEU_1980"]
dt$LRP.3[dt$Es=="DEU_1980"]=dt$RLRPP3[dt$Es=="DEU_1980"]

dt$LRP.1[dt$Es=="DEU_1983"]=dt$RLRPP2[dt$Es=="DEU_1983"]
dt$LRP.2[dt$Es=="DEU_1983"]=dt$RLRPP1[dt$Es=="DEU_1983"]
dt$LRP.3[dt$Es=="DEU_1983"]=dt$RLRPP3[dt$Es=="DEU_1983"]

dt$LRP.1[dt$Es=="DEU_1987"]=dt$RLRPP2[dt$Es=="DEU_1987"]
dt$LRP.2[dt$Es=="DEU_1987"]=dt$RLRPP1[dt$Es=="DEU_1987"]
dt$LRP.3[dt$Es=="DEU_1987"]=dt$RLRPP3[dt$Es=="DEU_1987"]

dt$LRP.1[dt$Es=="DEU_1990"]=dt$RLRPP2[dt$Es=="DEU_1990"]
dt$LRP.2[dt$Es=="DEU_1990"]=dt$RLRPP1[dt$Es=="DEU_1990"]
dt$LRP.3[dt$Es=="DEU_1990"]=dt$RLRPP3[dt$Es=="DEU_1990"]
dt$LRP.4[dt$Es=="DEU_1990"]=dt$RLRPP5[dt$Es=="DEU_1990"]

dt$LRP.1[dt$Es=="DEU_1994"]=dt$RLRPP2[dt$Es=="DEU_1994"]
dt$LRP.2[dt$Es=="DEU_1994"]=dt$RLRPP1[dt$Es=="DEU_1994"]
dt$LRP.3[dt$Es=="DEU_1994"]=dt$RLRPP4[dt$Es=="DEU_1994"]
dt$LRP.4[dt$Es=="DEU_1994"]=dt$RLRPP3[dt$Es=="DEU_1994"]
dt$LRP.5[dt$Es=="DEU_1994"]=dt$RLRPP5[dt$Es=="DEU_1994"]

dt$LRP.1[dt$Es=="DEU_1998"]=dt$RLRPP1[dt$Es=="DEU_1998"]
dt$LRP.2[dt$Es=="DEU_1998"]=dt$RLRPP2[dt$Es=="DEU_1998"]
dt$LRP.3[dt$Es=="DEU_1998"]=dt$RLRPP4[dt$Es=="DEU_1998"]
dt$LRP.4[dt$Es=="DEU_1998"]=dt$RLRPP3[dt$Es=="DEU_1998"]
dt$LRP.5[dt$Es=="DEU_1998"]=dt$RLRPP5[dt$Es=="DEU_1998"]

dt$LRP.1[dt$Es=="DEU_2009"]=dt$RLRPP2[dt$Es=="DEU_2009"]
dt$LRP.2[dt$Es=="DEU_2009"]=dt$RLRPP1[dt$Es=="DEU_2009"]
dt$LRP.3[dt$Es=="DEU_2009"]=dt$RLRPP3[dt$Es=="DEU_2009"]
dt$LRP.4[dt$Es=="DEU_2009"]=dt$RLRPP5[dt$Es=="DEU_2009"]
dt$LRP.5[dt$Es=="DEU_2009"]=dt$RLRPP4[dt$Es=="DEU_2009"]

dt$LRP.1[dt$Es=="DEU_2013"]=dt$RLRPP2[dt$Es=="DEU_2013"]
dt$LRP.2[dt$Es=="DEU_2013"]=dt$RLRPP1[dt$Es=="DEU_2013"]
dt$LRP.3[dt$Es=="DEU_2013"]=dt$RLRPP5[dt$Es=="DEU_2013"]
dt$LRP.4[dt$Es=="DEU_2013"]=dt$RLRPP4[dt$Es=="DEU_2013"]
dt$LRP.5[dt$Es=="DEU_2013"]=dt$RLRPP3[dt$Es=="DEU_2013"]

dt$LRP.1[dt$Es=="FIN_2007"]=dt$RLRPP1[dt$Es=="FIN_2007"]
dt$LRP.2[dt$Es=="FIN_2007"]=dt$RLRPP3[dt$Es=="FIN_2007"]
dt$LRP.3[dt$Es=="FIN_2007"]=dt$RLRPP2[dt$Es=="FIN_2007"]
dt$LRP.4[dt$Es=="FIN_2007"]=dt$RLRPP4[dt$Es=="FIN_2007"]
dt$LRP.5[dt$Es=="FIN_2007"]=dt$RLRPP5[dt$Es=="FIN_2007"]
dt$LRP.6[dt$Es=="FIN_2007"]=dt$RLRPP7[dt$Es=="FIN_2007"]

dt$LRP.1[dt$Es=="FIN_2011"]=dt$RLRPP3[dt$Es=="FIN_2011"]
dt$LRP.2[dt$Es=="FIN_2011"]=dt$RLRPP2[dt$Es=="FIN_2011"]
dt$LRP.3[dt$Es=="FIN_2011"]=dt$RLRPP8[dt$Es=="FIN_2011"]
dt$LRP.4[dt$Es=="FIN_2011"]=dt$RLRPP1[dt$Es=="FIN_2011"]
dt$LRP.5[dt$Es=="FIN_2011"]=dt$RLRPP4[dt$Es=="FIN_2011"]
dt$LRP.6[dt$Es=="FIN_2011"]=dt$RLRPP5[dt$Es=="FIN_2011"]

dt$LRP.1[dt$Es=="GRC_2009"]=dt$RLRPP1[dt$Es=="GRC_2009"]
dt$LRP.2[dt$Es=="GRC_2009"]=dt$RLRPP2[dt$Es=="GRC_2009"]
dt$LRP.3[dt$Es=="GRC_2009"]=dt$RLRPP3[dt$Es=="GRC_2009"]
dt$LRP.4[dt$Es=="GRC_2009"]=dt$RLRPP4[dt$Es=="GRC_2009"]
dt$LRP.5[dt$Es=="GRC_2009"]=dt$RLRPP5[dt$Es=="GRC_2009"]
dt$LRP.6[dt$Es=="GRC_2009"]=dt$RLRPP6[dt$Es=="GRC_2009"]

dt$LRP.1[dt$Es=="GRC_2012"]=dt$RLRPP1[dt$Es=="GRC_2012"]
dt$LRP.2[dt$Es=="GRC_2012"]=dt$RLRPP2[dt$Es=="GRC_2012"]
dt$LRP.3[dt$Es=="GRC_2012"]=dt$RLRPP3[dt$Es=="GRC_2012"]
dt$LRP.4[dt$Es=="GRC_2012"]=dt$RLRPP4[dt$Es=="GRC_2012"]
dt$LRP.5[dt$Es=="GRC_2012"]=dt$RLRPP5[dt$Es=="GRC_2012"]
dt$LRP.6[dt$Es=="GRC_2012"]=dt$RLRPP6[dt$Es=="GRC_2012"]

dt$LRP.1[dt$Es=="IRL_2007"]=dt$RLRPP1[dt$Es=="IRL_2007"]
dt$LRP.2[dt$Es=="IRL_2007"]=dt$RLRPP2[dt$Es=="IRL_2007"]
dt$LRP.3[dt$Es=="IRL_2007"]=dt$RLRPP4[dt$Es=="IRL_2007"]
dt$LRP.4[dt$Es=="IRL_2007"]=dt$RLRPP3[dt$Es=="IRL_2007"]
dt$LRP.5[dt$Es=="IRL_2007"]=dt$RLRPP6[dt$Es=="IRL_2007"]
dt$LRP.6[dt$Es=="IRL_2007"]=dt$RLRPP5[dt$Es=="IRL_2007"]

dt$LRP.1[dt$Es=="IRL_2011"]=dt$RLRPP2[dt$Es=="IRL_2011"]
dt$LRP.2[dt$Es=="IRL_2011"]=dt$RLRPP3[dt$Es=="IRL_2011"]
dt$LRP.3[dt$Es=="IRL_2011"]=dt$RLRPP1[dt$Es=="IRL_2011"]
dt$LRP.4[dt$Es=="IRL_2011"]=dt$RLRPP4[dt$Es=="IRL_2011"]
dt$LRP.5[dt$Es=="IRL_2011"]=dt$RLRPP5[dt$Es=="IRL_2011"]#

dt$LRP.1[dt$Es=="ISL_1999"]=dt$RLRPP3[dt$Es=="ISL_1999"]
dt$LRP.2[dt$Es=="ISL_1999"]=dt$RLRPP1[dt$Es=="ISL_1999"]
dt$LRP.3[dt$Es=="ISL_1999"]=dt$RLRPP2[dt$Es=="ISL_1999"]
dt$LRP.4[dt$Es=="ISL_1999"]=dt$RLRPP4[dt$Es=="ISL_1999"]
dt$LRP.5[dt$Es=="ISL_1999"]=dt$RLRPP5[dt$Es=="ISL_1999"]

dt$LRP.1[dt$Es=="ISL_2007"]=dt$RLRPP3[dt$Es=="ISL_2007"]
dt$LRP.2[dt$Es=="ISL_2007"]=dt$RLRPP1[dt$Es=="ISL_2007"]
dt$LRP.3[dt$Es=="ISL_2007"]=dt$RLRPP4[dt$Es=="ISL_2007"]
dt$LRP.4[dt$Es=="ISL_2007"]=dt$RLRPP2[dt$Es=="ISL_2007"]
dt$LRP.5[dt$Es=="ISL_2007"]=dt$RLRPP5[dt$Es=="ISL_2007"]
dt$LRP.6[dt$Es=="ISL_2007"]=dt$RLRPP6[dt$Es=="ISL_2007"]#

dt$LRP.1[dt$Es=="ISL_2009"]=dt$RLRPP1[dt$Es=="ISL_2009"]
dt$LRP.2[dt$Es=="ISL_2009"]=dt$RLRPP3[dt$Es=="ISL_2009"]
dt$LRP.3[dt$Es=="ISL_2009"]=dt$RLRPP4[dt$Es=="ISL_2009"]
dt$LRP.4[dt$Es=="ISL_2009"]=dt$RLRPP2[dt$Es=="ISL_2009"]
dt$LRP.5[dt$Es=="ISL_2009"]=dt$RLRPP5[dt$Es=="ISL_2009"]#
dt$LRP.6[dt$Es=="ISL_2009"]=dt$RLRPP6[dt$Es=="ISL_2009"]#

dt$LRP.1[dt$Es=="ISL_2013"]=dt$RLRPP3[dt$Es=="ISL_2013"]
dt$LRP.2[dt$Es=="ISL_2013"]=dt$RLRPP2[dt$Es=="ISL_2013"]
dt$LRP.3[dt$Es=="ISL_2013"]=dt$RLRPP1[dt$Es=="ISL_2013"]
dt$LRP.4[dt$Es=="ISL_2013"]=dt$RLRPP4[dt$Es=="ISL_2013"]
dt$LRP.5[dt$Es=="ISL_2013"]=dt$RLRPP5[dt$Es=="ISL_2013"]
dt$LRP.6[dt$Es=="ISL_2013"]=dt$RLRPP6[dt$Es=="ISL_2013"]

dt$LRP.1[dt$Es=="NLD_1994"]=dt$RLRPP1[dt$Es=="NLD_1994"]
dt$LRP.2[dt$Es=="NLD_1994"]=dt$RLRPP2[dt$Es=="NLD_1994"]
dt$LRP.3[dt$Es=="NLD_1994"]=dt$RLRPP3[dt$Es=="NLD_1994"]
dt$LRP.4[dt$Es=="NLD_1994"]=dt$RLRPP4[dt$Es=="NLD_1994"]

dt$LRP.1[dt$Es=="NLD_1998"]=dt$RLRPP1[dt$Es=="NLD_1998"]#EV 4 parties, CSES 6 parties coded
dt$LRP.2[dt$Es=="NLD_1998"]=dt$RLRPP3[dt$Es=="NLD_1998"]
dt$LRP.3[dt$Es=="NLD_1998"]=dt$RLRPP2[dt$Es=="NLD_1998"]
dt$LRP.4[dt$Es=="NLD_1998"]=dt$RLRPP4[dt$Es=="NLD_1998"]
dt$LRP.5[dt$Es=="NLD_1998"]=dt$RLRPP5[dt$Es=="NLD_1998"]
dt$LRP.6[dt$Es=="NLD_1998"]=dt$RLRPP7[dt$Es=="NLD_1998"]#

dt$LRP.1[dt$Es=="NOR_1981"]=dt$RLRPP2[dt$Es=="NOR_1981"]
dt$LRP.2[dt$Es=="NOR_1981"]=dt$RLRPP6[dt$Es=="NOR_1981"]
dt$LRP.3[dt$Es=="NOR_1981"]=dt$RLRPP4[dt$Es=="NOR_1981"]
dt$LRP.4[dt$Es=="NOR_1981"]=dt$RLRPP5[dt$Es=="NOR_1981"]
dt$LRP.5[dt$Es=="NOR_1981"]=dt$RLRPP1[dt$Es=="NOR_1981"]
dt$LRP.6[dt$Es=="NOR_1981"]=dt$RLRPP7[dt$Es=="NOR_1981"]

dt$LRP.1[dt$Es=="NOR_1985"]=dt$RLRPP2[dt$Es=="NOR_1985"]
dt$LRP.2[dt$Es=="NOR_1985"]=dt$RLRPP6[dt$Es=="NOR_1985"]
dt$LRP.3[dt$Es=="NOR_1985"]=dt$RLRPP4[dt$Es=="NOR_1985"]
dt$LRP.4[dt$Es=="NOR_1985"]=dt$RLRPP5[dt$Es=="NOR_1985"]
dt$LRP.5[dt$Es=="NOR_1985"]=dt$RLRPP1[dt$Es=="NOR_1985"]
dt$LRP.6[dt$Es=="NOR_1985"]=dt$RLRPP7[dt$Es=="NOR_1985"]

dt$LRP.1[dt$Es=="NOR_1989"]=dt$RLRPP2[dt$Es=="NOR_1989"]
dt$LRP.2[dt$Es=="NOR_1989"]=dt$RLRPP6[dt$Es=="NOR_1989"]
dt$LRP.3[dt$Es=="NOR_1989"]=dt$RLRPP7[dt$Es=="NOR_1989"]
dt$LRP.4[dt$Es=="NOR_1989"]=dt$RLRPP1[dt$Es=="NOR_1989"]
dt$LRP.5[dt$Es=="NOR_1989"]=dt$RLRPP4[dt$Es=="NOR_1989"]
dt$LRP.6[dt$Es=="NOR_1989"]=dt$RLRPP5[dt$Es=="NOR_1989"]

dt$LRP.1[dt$Es=="NOR_1993"]=dt$RLRPP2[dt$Es=="NOR_1993"]
dt$LRP.2[dt$Es=="NOR_1993"]=dt$RLRPP5[dt$Es=="NOR_1993"]
dt$LRP.3[dt$Es=="NOR_1993"]=dt$RLRPP6[dt$Es=="NOR_1993"]
dt$LRP.4[dt$Es=="NOR_1993"]=dt$RLRPP1[dt$Es=="NOR_1993"]
dt$LRP.5[dt$Es=="NOR_1993"]=dt$RLRPP4[dt$Es=="NOR_1993"]
dt$LRP.6[dt$Es=="NOR_1993"]=dt$RLRPP7[dt$Es=="NOR_1993"]

dt$LRP.1[dt$Es=="NOR_1997"]=dt$RLRPP2[dt$Es=="NOR_1997"]
dt$LRP.2[dt$Es=="NOR_1997"]=dt$RLRPP7[dt$Es=="NOR_1997"]
dt$LRP.3[dt$Es=="NOR_1997"]=dt$RLRPP4[dt$Es=="NOR_1997"]
dt$LRP.4[dt$Es=="NOR_1997"]=dt$RLRPP6[dt$Es=="NOR_1997"]
dt$LRP.5[dt$Es=="NOR_1997"]=dt$RLRPP5[dt$Es=="NOR_1997"]
dt$LRP.6[dt$Es=="NOR_1997"]=dt$RLRPP1[dt$Es=="NOR_1997"]

dt$LRP.1[dt$Es=="NOR_2005"]=dt$RLRPP2[dt$Es=="NOR_2005"]
dt$LRP.2[dt$Es=="NOR_2005"]=dt$RLRPP7[dt$Es=="NOR_2005"]
dt$LRP.3[dt$Es=="NOR_2005"]=dt$RLRPP6[dt$Es=="NOR_2005"]
dt$LRP.4[dt$Es=="NOR_2005"]=dt$RLRPP1[dt$Es=="NOR_2005"]
dt$LRP.5[dt$Es=="NOR_2005"]=dt$RLRPP4[dt$Es=="NOR_2005"]
dt$LRP.6[dt$Es=="NOR_2005"]=dt$RLRPP5[dt$Es=="NOR_2005"]

dt$LRP.1[dt$Es=="PRT_2002"]=dt$RLRPP4[dt$Es=="PRT_2002"]
dt$LRP.2[dt$Es=="PRT_2002"]=dt$RLRPP5[dt$Es=="PRT_2002"]
dt$LRP.3[dt$Es=="PRT_2002"]=dt$RLRPP2[dt$Es=="PRT_2002"]
dt$LRP.4[dt$Es=="PRT_2002"]=dt$RLRPP3[dt$Es=="PRT_2002"]
dt$LRP.5[dt$Es=="PRT_2002"]=dt$RLRPP1[dt$Es=="PRT_2002"]
dt$LRP.6[dt$Es=="PRT_2002"]=dt$RLRPP6[dt$Es=="PRT_2002"]

dt$LRP.1[dt$Es=="PRT_2009"]=dt$RLRPP5[dt$Es=="PRT_2009"]#osym4-5  inverted!?
dt$LRP.2[dt$Es=="PRT_2009"]=dt$RLRPP4[dt$Es=="PRT_2009"]
dt$LRP.3[dt$Es=="PRT_2009"]=dt$RLRPP2[dt$Es=="PRT_2009"]
dt$LRP.4[dt$Es=="PRT_2009"]=dt$RLRPP1[dt$Es=="PRT_2009"]
dt$LRP.5[dt$Es=="PRT_2009"]=dt$RLRPP3[dt$Es=="PRT_2009"]

dt$LRP.1[dt$Es=="SWE_1982"]=dt$RLRPP2[dt$Es=="SWE_1982"]
dt$LRP.2[dt$Es=="SWE_1982"]=dt$RLRPP5[dt$Es=="SWE_1982"]
dt$LRP.3[dt$Es=="SWE_1982"]=dt$RLRPP3[dt$Es=="SWE_1982"]
dt$LRP.4[dt$Es=="SWE_1982"]=dt$RLRPP4[dt$Es=="SWE_1982"]
dt$LRP.5[dt$Es=="SWE_1982"]=dt$RLRPP1[dt$Es=="SWE_1982"]

dt$LRP.1[dt$Es=="SWE_1985"]=dt$RLRPP2[dt$Es=="SWE_1985"]
dt$LRP.2[dt$Es=="SWE_1985"]=dt$RLRPP5[dt$Es=="SWE_1985"]
dt$LRP.3[dt$Es=="SWE_1985"]=dt$RLRPP4[dt$Es=="SWE_1985"]
dt$LRP.4[dt$Es=="SWE_1985"]=dt$RLRPP3[dt$Es=="SWE_1985"]
dt$LRP.5[dt$Es=="SWE_1985"]=dt$RLRPP1[dt$Es=="SWE_1985"]
dt$LRP.6[dt$Es=="SWE_1985"]=dt$RLRPP6[dt$Es=="SWE_1985"]

dt$LRP.1[dt$Es=="SWE_1988"]=dt$RLRPP2[dt$Es=="SWE_1988"]
dt$LRP.2[dt$Es=="SWE_1988"]=dt$RLRPP5[dt$Es=="SWE_1988"]
dt$LRP.3[dt$Es=="SWE_1988"]=dt$RLRPP4[dt$Es=="SWE_1988"]
dt$LRP.4[dt$Es=="SWE_1988"]=dt$RLRPP3[dt$Es=="SWE_1988"]
dt$LRP.5[dt$Es=="SWE_1988"]=dt$RLRPP1[dt$Es=="SWE_1988"]

dt$LRP.1[dt$Es=="SWE_1991"]=dt$RLRPP2[dt$Es=="SWE_1991"]
dt$LRP.2[dt$Es=="SWE_1991"]=dt$RLRPP5[dt$Es=="SWE_1991"]
dt$LRP.3[dt$Es=="SWE_1991"]=dt$RLRPP4[dt$Es=="SWE_1991"]
dt$LRP.4[dt$Es=="SWE_1991"]=dt$RLRPP3[dt$Es=="SWE_1991"]
dt$LRP.5[dt$Es=="SWE_1991"]=dt$RLRPP6[dt$Es=="SWE_1991"]
dt$LRP.6[dt$Es=="SWE_1991"]=dt$RLRPP9[dt$Es=="SWE_1991"]

dt$LRP.1[dt$Es=="SWE_1994"]=dt$RLRPP2[dt$Es=="SWE_1994"]
dt$LRP.2[dt$Es=="SWE_1994"]=dt$RLRPP5[dt$Es=="SWE_1994"]
dt$LRP.3[dt$Es=="SWE_1994"]=dt$RLRPP3[dt$Es=="SWE_1994"]
dt$LRP.4[dt$Es=="SWE_1994"]=dt$RLRPP4[dt$Es=="SWE_1994"]
dt$LRP.5[dt$Es=="SWE_1994"]=dt$RLRPP1[dt$Es=="SWE_1994"]
dt$LRP.6[dt$Es=="SWE_1994"]=dt$RLRPP7[dt$Es=="SWE_1994"]

dt$LRP.1[dt$Es=="SWE_1998"]=dt$RLRPP2[dt$Es=="SWE_1998"]
dt$LRP.2[dt$Es=="SWE_1998"]=dt$RLRPP5[dt$Es=="SWE_1998"]
dt$LRP.3[dt$Es=="SWE_1998"]=dt$RLRPP1[dt$Es=="SWE_1998"]
dt$LRP.4[dt$Es=="SWE_1998"]=dt$RLRPP6[dt$Es=="SWE_1998"]
dt$LRP.5[dt$Es=="SWE_1998"]=dt$RLRPP3[dt$Es=="SWE_1998"]
dt$LRP.6[dt$Es=="SWE_1998"]=dt$RLRPP4[dt$Es=="SWE_1998"]

dt$LRP.1[dt$Es=="SWE_2006"]=dt$RLRPP2[dt$Es=="SWE_2006"]
dt$LRP.2[dt$Es=="SWE_2006"]=dt$RLRPP5[dt$Es=="SWE_2006"]
dt$LRP.3[dt$Es=="SWE_2006"]=dt$RLRPP3[dt$Es=="SWE_2006"]
dt$LRP.4[dt$Es=="SWE_2006"]=dt$RLRPP4[dt$Es=="SWE_2006"]
dt$LRP.5[dt$Es=="SWE_2006"]=dt$RLRPP6[dt$Es=="SWE_2006"]
dt$LRP.6[dt$Es=="SWE_2006"]=dt$RLRPP1[dt$Es=="SWE_2006"]

dt$LRP.1=as.numeric(dt$LRP.1)#recode as numeric:
dt$LRP.2=as.numeric(dt$LRP.2)
dt$LRP.3=as.numeric(dt$LRP.3)
dt$LRP.4=as.numeric(dt$LRP.4)
dt$LRP.5=as.numeric(dt$LRP.5)
dt$LRP.6=as.numeric(dt$LRP.6)

dt$LRP.1[dt$LRP.1>1]=NA#exclude NAs coded 900s:
dt$LRP.2[dt$LRP.2>1]=NA
dt$LRP.3[dt$LRP.3>1]=NA
dt$LRP.4[dt$LRP.4>1]=NA
dt$LRP.5[dt$LRP.5>1]=NA
dt$LRP.6[dt$LRP.6>1]=NA

dt$LRP.1=dt$LRP.1*10#recode to 0-10 scale:
dt$LRP.2=dt$LRP.2*10
dt$LRP.3=dt$LRP.3*10
dt$LRP.4=dt$LRP.4*10
dt$LRP.5=dt$LRP.5*10
dt$LRP.6=dt$LRP.6*10


#NP:----
dt$NP=NA
dt$NP[dt$Es=="ESP_1979"]=4
dt$NP[dt$Es=="ESP_1982"]=5
dt$NP[dt$Es=="ESP_1993"]=4
dt$NP[dt$Es=="ESP_2004"]=3
dt$NP[dt$Es=="EST_2011"]=6
dt$NP[dt$Es=="FIN_2003"]=6
dt$NP[dt$Es=="GBR_2005"]=3
dt$NP[dt$Es=="GRC_1985"]=4
dt$NP[dt$Es=="GRC_1989"]=3
dt$NP[dt$Es=="GRC_1990"]=3
dt$NP[dt$Es=="GRC_1996"]=5
dt$NP[dt$Es=="GRC_2000"]=4
dt$NP[dt$Es=="GRC_2004"]=4
dt$NP[dt$Es=="GRC_2007"]=5
dt$NP[dt$Es=="HUN_1998"]=6
dt$NP[dt$Es=="HUN_2006"]=4
dt$NP[dt$Es=="IRL_2002"]=6
dt$NP[dt$Es=="ISL_1987"]=5
dt$NP[dt$Es=="ISL_1991"]=5
dt$NP[dt$Es=="ISL_1995"]=6
dt$NP[dt$Es=="ISL_2003"]=5
dt$NP[dt$Es=="ITA_2006"]=6
dt$NP[dt$Es=="ITA_2008"]=6
dt$NP[dt$Es=="LTU_2000"]=4
dt$NP[dt$Es=="NLD_2002"]=6
dt$NP[dt$Es=="NOR_2001"]=6
dt$NP[dt$Es=="PRT_2005"]=5
dt$NP[dt$Es=="SVN_2000"]=6
dt$NP[dt$Es=="SWE_1979"]=5
dt$NP[dt$Es=="SWE_2002"]=6
dt$NP[dt$Es=="SWE_2010"]=6

#common elections TEV-CSES (minus exclusions, see "check common elections"):
dt$NP[dt$Es=="DEU_1961"]=3
dt$NP[dt$Es=="DEU_1965"]=3
dt$NP[dt$Es=="DEU_1976"]=3
dt$NP[dt$Es=="DEU_1980"]=3
dt$NP[dt$Es=="DEU_1983"]=3
dt$NP[dt$Es=="DEU_1987"]=3
dt$NP[dt$Es=="DEU_1990"]=4
dt$NP[dt$Es=="DEU_1994"]=5
dt$NP[dt$Es=="DEU_1998"]=5
dt$NP[dt$Es=="DEU_2009"]=5
dt$NP[dt$Es=="DEU_2013"]=5
dt$NP[dt$Es=="FIN_2007"]=6
dt$NP[dt$Es=="FIN_2011"]=6
dt$NP[dt$Es=="GRC_2009"]=6
dt$NP[dt$Es=="GRC_2012"]=6
dt$NP[dt$Es=="IRL_2007"]=6
dt$NP[dt$Es=="IRL_2011"]=5
dt$NP[dt$Es=="ISL_1999"]=5
dt$NP[dt$Es=="ISL_2007"]=6
dt$NP[dt$Es=="ISL_2009"]=6
dt$NP[dt$Es=="ISL_2013"]=6
dt$NP[dt$Es=="NLD_1994"]=4
dt$NP[dt$Es=="NLD_1998"]=6
dt$NP[dt$Es=="NOR_1981"]=6
dt$NP[dt$Es=="NOR_1985"]=6
dt$NP[dt$Es=="NOR_1989"]=6
dt$NP[dt$Es=="NOR_1993"]=6
dt$NP[dt$Es=="NOR_1997"]=6
dt$NP[dt$Es=="NOR_2005"]=6
dt$NP[dt$Es=="PRT_2002"]=6
dt$NP[dt$Es=="PRT_2009"]=5
dt$NP[dt$Es=="SWE_1982"]=5
dt$NP[dt$Es=="SWE_1985"]=6
dt$NP[dt$Es=="SWE_1988"]=5
dt$NP[dt$Es=="SWE_1991"]=6
dt$NP[dt$Es=="SWE_1994"]=6
dt$NP[dt$Es=="SWE_1998"]=6
dt$NP[dt$Es=="SWE_2006"]=6


#CoalCode:----
dt$CoalCode=NA
dt$CoalCode[dt$Es=="ESP_1979"]=0
dt$CoalCode[dt$Es=="ESP_1982"]=0
dt$CoalCode[dt$Es=="ESP_1993"]=0
dt$CoalCode[dt$Es=="ESP_2004"]=0
dt$CoalCode[dt$Es=="EST_2011"]=0
dt$CoalCode[dt$Es=="FIN_2003"]=0
dt$CoalCode[dt$Es=="GBR_2005"]=0
dt$CoalCode[dt$Es=="GRC_1985"]=0
dt$CoalCode[dt$Es=="GRC_1990"]=1
dt$CoalCode[dt$Es=="GRC_1990"]=0
dt$CoalCode[dt$Es=="GRC_1996"]=0
dt$CoalCode[dt$Es=="GRC_2000"]=0
dt$CoalCode[dt$Es=="GRC_2004"]=0
dt$CoalCode[dt$Es=="GRC_2007"]=0
dt$CoalCode[dt$Es=="HUN_1998"]=0
dt$CoalCode[dt$Es=="HUN_2006"]=0
dt$CoalCode[dt$Es=="IRL_2002"]=0
dt$CoalCode[dt$Es=="ISL_1987"]=0
dt$CoalCode[dt$Es=="ISL_1991"]=0
dt$CoalCode[dt$Es=="ISL_1995"]=0
dt$CoalCode[dt$Es=="ISL_2003"]=0
dt$CoalCode[dt$Es=="ITA_2006"]=1
dt$CoalCode[dt$Es=="ITA_2008"]=1
dt$CoalCode[dt$Es=="LTU_2000"]=0
dt$CoalCode[dt$Es=="NLD_2002"]=0
dt$CoalCode[dt$Es=="NOR_2001"]=0
dt$CoalCode[dt$Es=="PRT_2005"]=0
dt$CoalCode[dt$Es=="SVN_2000"]=0
dt$CoalCode[dt$Es=="SWE_1979"]=0
dt$CoalCode[dt$Es=="SWE_2002"]=0
dt$CoalCode[dt$Es=="SWE_2010"]=0

#common elections TEV-CSES (minus exclusions, see "check common elections"):
dt$CoalCode[dt$Es=="DEU_1961"]=1
dt$CoalCode[dt$Es=="DEU_1965"]=1
dt$CoalCode[dt$Es=="DEU_1976"]=1
dt$CoalCode[dt$Es=="DEU_1980"]=1
dt$CoalCode[dt$Es=="DEU_1983"]=1
dt$CoalCode[dt$Es=="DEU_1987"]=1
dt$CoalCode[dt$Es=="DEU_1990"]=1
dt$CoalCode[dt$Es=="DEU_1994"]=1
dt$CoalCode[dt$Es=="DEU_1998"]=1
dt$CoalCode[dt$Es=="DEU_2009"]=1
dt$CoalCode[dt$Es=="DEU_2013"]=1
dt$CoalCode[dt$Es=="FIN_2007"]=0
dt$CoalCode[dt$Es=="FIN_2011"]=0
dt$CoalCode[dt$Es=="GRC_2009"]=0
dt$CoalCode[dt$Es=="GRC_2012"]=0
dt$CoalCode[dt$Es=="IRL_2007"]=0
dt$CoalCode[dt$Es=="IRL_2011"]=0
dt$CoalCode[dt$Es=="ISL_1999"]=0
dt$CoalCode[dt$Es=="ISL_2007"]=0
dt$CoalCode[dt$Es=="ISL_2009"]=0
dt$CoalCode[dt$Es=="ISL_2013"]=0
dt$CoalCode[dt$Es=="NLD_1994"]=0
dt$CoalCode[dt$Es=="NLD_1998"]=0
dt$CoalCode[dt$Es=="NOR_1981"]=0
dt$CoalCode[dt$Es=="NOR_1985"]=0
dt$CoalCode[dt$Es=="NOR_1989"]=0
dt$CoalCode[dt$Es=="NOR_1993"]=0
dt$CoalCode[dt$Es=="NOR_1997"]=0
dt$CoalCode[dt$Es=="NOR_2005"]=0
dt$CoalCode[dt$Es=="PRT_2002"]=0
dt$CoalCode[dt$Es=="PRT_2009"]=0
dt$CoalCode[dt$Es=="SWE_1982"]=0
dt$CoalCode[dt$Es=="SWE_1985"]=0
dt$CoalCode[dt$Es=="SWE_1988"]=0
dt$CoalCode[dt$Es=="SWE_1991"]=0
dt$CoalCode[dt$Es=="SWE_1994"]=0
dt$CoalCode[dt$Es=="SWE_1998"]=0
dt$CoalCode[dt$Es=="SWE_2006"]=0


#Rwd demographic weights:----

dt$OWEIGHT1[dt$OWEIGHT1==999]=1#non coded weight in elections to NA
dt$OWEIGHT2[dt$OWEIGHT2==999]=1
dt$OWEIGHT3[dt$OWEIGHT3==999]=1
dt$OWEIGHT4[dt$OWEIGHT4==999]=1
dt$OWEIGHT1[dt$OWEIGHT1==0]=1#missing respondent weight put to neutral weight of 1
dt$OWEIGHT2[dt$OWEIGHT2==0]=1
dt$OWEIGHT3[dt$OWEIGHT3==0]=1
dt$OWEIGHT4[dt$OWEIGHT4==0]=1
summary(dt$OWEIGHT1)
summary(dt$OWEIGHT2)
summary(dt$OWEIGHT3)
summary(dt$OWEIGHT4)

#code election specific weights:
dt$Rwd=1#Respondent Weight Demographic:
dt$Rwd[dt$Es=="DEU_1961"]=dt$OWEIGHT1[dt$Es=="DEU_1961"]
dt$Rwd[dt$Es=="DEU_1965"]=dt$OWEIGHT1[dt$Es=="DEU_1965"]
dt$Rwd[dt$Es=="DEU_1976"]=dt$OWEIGHT1[dt$Es=="DEU_1976"]
dt$Rwd[dt$Es=="DEU_1980"]=dt$OWEIGHT2[dt$Es=="DEU_1980"]
dt$Rwd[dt$Es=="DEU_1983"]=dt$OWEIGHT2[dt$Es=="DEU_1983"]
dt$Rwd[dt$Es=="DEU_1987"]=dt$OWEIGHT4[dt$Es=="DEU_1987"]
dt$Rwd[dt$Es=="DEU_1990"]=dt$OWEIGHT2[dt$Es=="DEU_1990"]
dt$Rwd[dt$Es=="DEU_1994"]=dt$OWEIGHT1[dt$Es=="DEU_1994"]
dt$Rwd[dt$Es=="DEU_1998"]=dt$OWEIGHT1[dt$Es=="DEU_1998"]
dt$Rwd[dt$Es=="DEU_2009"]=dt$OWEIGHT2[dt$Es=="DEU_2009"]
dt$Rwd[dt$Es=="DEU_2013"]=dt$OWEIGHT2[dt$Es=="DEU_2013"]
dt$Rwd[dt$Es=="ESP_1979"]=dt$OWEIGHT1[dt$Es=="ESP_1979"]
dt$Rwd[dt$Es=="ESP_1982"]=dt$OWEIGHT1[dt$Es=="ESP_1982"]
dt$Rwd[dt$Es=="FIN_2003"]=dt$OWEIGHT1[dt$Es=="FIN_2003"]
dt$Rwd[dt$Es=="FIN_2007"]=dt$OWEIGHT1[dt$Es=="FIN_2007"]
dt$Rwd[dt$Es=="GBR_2005"]=dt$OWEIGHT1[dt$Es=="GBR_2005"]
dt$Rwd[dt$Es=="IRL_2002"]=dt$OWEIGHT1[dt$Es=="IRL_2002"]
dt$Rwd[dt$Es=="ISL_1987"]=dt$OWEIGHT1[dt$Es=="ISL_1987"]
dt$Rwd[dt$Es=="PRT_2005"]=dt$OWEIGHT4[dt$Es=="PRT_2005"]


#Party family voted party:----
colnames(dt)[colnames(dt)=="CMPPRT"]="CMPid"#CMP party codes for party R voted for

#read CMP data and party Id match CSES-CMP:
dCMP=read.xlsx("MPDataset_MPDS2020b.xlsx")

dCMP=dCMP[,c(7,10)]#filter data and set variable names:
colnames(dCMP)[colnames(dCMP)=="party"]="CMPid"
colnames(dCMP)[colnames(dCMP)=="parfam"]="Pf"
dCMP=dCMP[!duplicated(dCMP$CMPid),]

#code party family vote choice (VPf):
dt=merge(dt, dCMP, by = c("CMPid"), all.x=T)
colnames(dt)[colnames(dt)=="Pf"]="VPf"
# dt=dt[,-which(names(dt) %in% c("CMPid"))]


#CMPid.i:----

dt$CMPid.1=NA#create Pl:
dt$CMPid.2=NA
dt$CMPid.3=NA
dt$CMPid.4=NA
dt$CMPid.5=NA
dt$CMPid.6=NA

dt$CMPid.1[dt$Es=="ESP_1979"]=33430#assign CMPid by party size order for parties with likability:
dt$CMPid.2[dt$Es=="ESP_1979"]=33320
dt$CMPid.3[dt$Es=="ESP_1979"]=33220
dt$CMPid.4[dt$Es=="ESP_1979"]=33610

dt$CMPid.1[dt$Es=="ESP_1982"]=33320
dt$CMPid.2[dt$Es=="ESP_1982"]=33610
dt$CMPid.3[dt$Es=="ESP_1982"]=33430
dt$CMPid.4[dt$Es=="ESP_1982"]=33220
dt$CMPid.5[dt$Es=="ESP_1982"]=33512

dt$CMPid.1[dt$Es=="ESP_1993"]=33320
dt$CMPid.2[dt$Es=="ESP_1993"]=33610
dt$CMPid.3[dt$Es=="ESP_1993"]=33220
dt$CMPid.4[dt$Es=="ESP_1993"]=33512

dt$CMPid.1[dt$Es=="ESP_2004"]=33320
dt$CMPid.2[dt$Es=="ESP_2004"]=33610
dt$CMPid.3[dt$Es=="ESP_2004"]=33220

dt$CMPid.1[dt$Es=="EST_2011"]=83430
dt$CMPid.2[dt$Es=="EST_2011"]=83411
dt$CMPid.3[dt$Es=="EST_2011"]=83611
dt$CMPid.4[dt$Es=="EST_2011"]=83410
dt$CMPid.5[dt$Es=="EST_2011"]=83110
dt$CMPid.6[dt$Es=="EST_2011"]=83612

dt$CMPid.1[dt$Es=="FIN_2003"]=14810
dt$CMPid.2[dt$Es=="FIN_2003"]=14320
dt$CMPid.3[dt$Es=="FIN_2003"]=14620
dt$CMPid.4[dt$Es=="FIN_2003"]=14223
dt$CMPid.5[dt$Es=="FIN_2003"]=14110
dt$CMPid.6[dt$Es=="FIN_2003"]=14901

dt$CMPid.1[dt$Es=="GBR_2005"]=51320
dt$CMPid.2[dt$Es=="GBR_2005"]=51620
dt$CMPid.3[dt$Es=="GBR_2005"]=51421

dt$CMPid.1[dt$Es=="GRC_1985"]=34313
dt$CMPid.2[dt$Es=="GRC_1985"]=34511
dt$CMPid.3[dt$Es=="GRC_1985"]=34210
dt$CMPid.4[dt$Es=="GRC_1985"]=99990

dt$CMPid.1[dt$Es=="GRC_1989"]=34511
dt$CMPid.2[dt$Es=="GRC_1989"]=34313
dt$CMPid.3[dt$Es=="GRC_1989"]=34211

dt$CMPid.1[dt$Es=="GRC_1990"]=34511
dt$CMPid.2[dt$Es=="GRC_1990"]=34313
dt$CMPid.3[dt$Es=="GRC_1990"]=34211

dt$CMPid.1[dt$Es=="GRC_1996"]=34313
dt$CMPid.2[dt$Es=="GRC_1996"]=34511
dt$CMPid.3[dt$Es=="GRC_1996"]=34210
dt$CMPid.4[dt$Es=="GRC_1996"]=34211
dt$CMPid.5[dt$Es=="GRC_1996"]=34314

dt$CMPid.1[dt$Es=="GRC_2000"]=34313
dt$CMPid.2[dt$Es=="GRC_2000"]=34511
dt$CMPid.3[dt$Es=="GRC_2000"]=34210
dt$CMPid.4[dt$Es=="GRC_2000"]=34211

dt$CMPid.1[dt$Es=="GRC_2004"]=34511
dt$CMPid.2[dt$Es=="GRC_2004"]=34313
dt$CMPid.3[dt$Es=="GRC_2004"]=34210
dt$CMPid.4[dt$Es=="GRC_2004"]=34020

dt$CMPid.1[dt$Es=="GRC_2007"]=34511
dt$CMPid.2[dt$Es=="GRC_2007"]=34313
dt$CMPid.3[dt$Es=="GRC_2007"]=34210
dt$CMPid.4[dt$Es=="GRC_2007"]=34020
dt$CMPid.5[dt$Es=="GRC_2007"]=34710

dt$CMPid.1[dt$Es=="HUN_1998"]=86429
dt$CMPid.2[dt$Es=="HUN_1998"]=86220
dt$CMPid.3[dt$Es=="HUN_1998"]=86810
dt$CMPid.4[dt$Es=="HUN_1998"]=86422
dt$CMPid.5[dt$Es=="HUN_1998"]=86521
dt$CMPid.6[dt$Es=="HUN_1998"]=86620

dt$CMPid.1[dt$Es=="HUN_2006"]=86220
dt$CMPid.2[dt$Es=="HUN_2006"]=86061
dt$CMPid.3[dt$Es=="HUN_2006"]=86422
dt$CMPid.4[dt$Es=="HUN_2006"]=86521

dt$CMPid.1[dt$Es=="IRL_2002"]=53620
dt$CMPid.2[dt$Es=="IRL_2002"]=53520
dt$CMPid.3[dt$Es=="IRL_2002"]=53320
dt$CMPid.4[dt$Es=="IRL_2002"]=53420
dt$CMPid.5[dt$Es=="IRL_2002"]=53110
dt$CMPid.6[dt$Es=="IRL_2002"]=53951

dt$CMPid.1[dt$Es=="ISL_1987"]=15620
dt$CMPid.2[dt$Es=="ISL_1987"]=15810
dt$CMPid.3[dt$Es=="ISL_1987"]=15320
dt$CMPid.4[dt$Es=="ISL_1987"]=15220
dt$CMPid.5[dt$Es=="ISL_1987"]=15621

dt$CMPid.1[dt$Es=="ISL_1991"]=15620
dt$CMPid.2[dt$Es=="ISL_1991"]=15810
dt$CMPid.3[dt$Es=="ISL_1991"]=15320
dt$CMPid.4[dt$Es=="ISL_1991"]=15220
dt$CMPid.5[dt$Es=="ISL_1991"]=15951

dt$CMPid.1[dt$Es=="ISL_1995"]=15620
dt$CMPid.2[dt$Es=="ISL_1995"]=15810
dt$CMPid.3[dt$Es=="ISL_1995"]=15220
dt$CMPid.4[dt$Es=="ISL_1995"]=15320
dt$CMPid.5[dt$Es=="ISL_1995"]=15323
dt$CMPid.6[dt$Es=="ISL_1995"]=15951

dt$CMPid.1[dt$Es=="ISL_2003"]=15620
dt$CMPid.2[dt$Es=="ISL_2003"]=15328
dt$CMPid.3[dt$Es=="ISL_2003"]=15810
dt$CMPid.4[dt$Es=="ISL_2003"]=15111
dt$CMPid.5[dt$Es=="ISL_2003"]=15420

dt$CMPid.1[dt$Es=="ITA_2006"]=32329#coalition==1
dt$CMPid.2[dt$Es=="ITA_2006"]=32610
dt$CMPid.3[dt$Es=="ITA_2006"]=32710
dt$CMPid.4[dt$Es=="ITA_2006"]=32530
dt$CMPid.5[dt$Es=="ITA_2006"]=32212
dt$CMPid.6[dt$Es=="ITA_2006"]=32720

dt$CMPid.1[dt$Es=="ITA_2008"]=32061#coalition==1
dt$CMPid.2[dt$Es=="ITA_2008"]=32440
dt$CMPid.3[dt$Es=="ITA_2008"]=32720
dt$CMPid.4[dt$Es=="ITA_2008"]=32530
dt$CMPid.5[dt$Es=="ITA_2008"]=32902
dt$CMPid.6[dt$Es=="ITA_2008"]=99991

dt$CMPid.1[dt$Es=="LTU_2000"]=88410
dt$CMPid.2[dt$Es=="LTU_2000"]=88422
dt$CMPid.3[dt$Es=="LTU_2000"]=88321
dt$CMPid.4[dt$Es=="LTU_2000"]=88620

dt$CMPid.1[dt$Es=="NLD_2002"]=22521
dt$CMPid.2[dt$Es=="NLD_2002"]=22720
dt$CMPid.3[dt$Es=="NLD_2002"]=22420
dt$CMPid.4[dt$Es=="NLD_2002"]=22320
dt$CMPid.5[dt$Es=="NLD_2002"]=22110
dt$CMPid.6[dt$Es=="NLD_2002"]=22220

dt$CMPid.1[dt$Es=="NOR_2001"]=12320
dt$CMPid.2[dt$Es=="NOR_2001"]=12620
dt$CMPid.3[dt$Es=="NOR_2001"]=12951
dt$CMPid.4[dt$Es=="NOR_2001"]=12221
dt$CMPid.5[dt$Es=="NOR_2001"]=12520
dt$CMPid.6[dt$Es=="NOR_2001"]=12810

dt$CMPid.1[dt$Es=="PRT_2005"]=35311
dt$CMPid.2[dt$Es=="PRT_2005"]=35313
dt$CMPid.3[dt$Es=="PRT_2005"]=35220#CMP codes the communist party instead than the coalition
dt$CMPid.4[dt$Es=="PRT_2005"]=35520
dt$CMPid.5[dt$Es=="PRT_2005"]=35211

dt$CMPid.1[dt$Es=="SVN_2000"]=97421
dt$CMPid.2[dt$Es=="SVN_2000"]=97330
dt$CMPid.3[dt$Es=="SVN_2000"]=97321
dt$CMPid.4[dt$Es=="SVN_2000"]=97521
dt$CMPid.5[dt$Es=="SVN_2000"]=97522
dt$CMPid.6[dt$Es=="SVN_2000"]=97951

dt$CMPid.1[dt$Es=="SWE_1979"]=11320
dt$CMPid.2[dt$Es=="SWE_1979"]=11620
dt$CMPid.3[dt$Es=="SWE_1979"]=11810
dt$CMPid.4[dt$Es=="SWE_1979"]=11420
dt$CMPid.5[dt$Es=="SWE_1979"]=11220

dt$CMPid.1[dt$Es=="SWE_2002"]=11320
dt$CMPid.2[dt$Es=="SWE_2002"]=11620
dt$CMPid.3[dt$Es=="SWE_2002"]=11420
dt$CMPid.4[dt$Es=="SWE_2002"]=11520
dt$CMPid.5[dt$Es=="SWE_2002"]=11220
dt$CMPid.6[dt$Es=="SWE_2002"]=11810

dt$CMPid.1[dt$Es=="SWE_2010"]=11320
dt$CMPid.2[dt$Es=="SWE_2010"]=11620
dt$CMPid.3[dt$Es=="SWE_2010"]=11110
dt$CMPid.4[dt$Es=="SWE_2010"]=11420
dt$CMPid.5[dt$Es=="SWE_2010"]=11810
dt$CMPid.6[dt$Es=="SWE_2010"]=11710

#common elections TEV-CSES (minus exclusions, see "check common elections"):
dt$CMPid.1[dt$Es=="DEU_1961"]=41521
dt$CMPid.2[dt$Es=="DEU_1961"]=41320
dt$CMPid.3[dt$Es=="DEU_1961"]=41420

dt$CMPid.1[dt$Es=="DEU_1965"]=41521
dt$CMPid.2[dt$Es=="DEU_1965"]=41320
dt$CMPid.3[dt$Es=="DEU_1965"]=41420

dt$CMPid.1[dt$Es=="DEU_1976"]=41521
dt$CMPid.2[dt$Es=="DEU_1976"]=41320
dt$CMPid.3[dt$Es=="DEU_1976"]=41420

dt$CMPid.1[dt$Es=="DEU_1980"]=41521
dt$CMPid.2[dt$Es=="DEU_1980"]=41320
dt$CMPid.3[dt$Es=="DEU_1980"]=41420

dt$CMPid.1[dt$Es=="DEU_1983"]=41521
dt$CMPid.2[dt$Es=="DEU_1983"]=41320
dt$CMPid.3[dt$Es=="DEU_1983"]=41420

dt$CMPid.1[dt$Es=="DEU_1987"]=41521
dt$CMPid.2[dt$Es=="DEU_1987"]=41320
dt$CMPid.3[dt$Es=="DEU_1987"]=41420

dt$CMPid.1[dt$Es=="DEU_1990"]=41521
dt$CMPid.2[dt$Es=="DEU_1990"]=41320
dt$CMPid.3[dt$Es=="DEU_1990"]=41420
#CMP coded Party of Democratic Socialism while TEV coded The Left / Gysi, correct is CMP:
dt$CMPid.4[dt$Es=="DEU_1990"]=41221#

dt$CMPid.1[dt$Es=="DEU_1994"]=41521
dt$CMPid.2[dt$Es=="DEU_1994"]=41320
dt$CMPid.3[dt$Es=="DEU_1994"]=41113
dt$CMPid.4[dt$Es=="DEU_1994"]=41420
#CMP coded Party of Democratic Socialism while TEV coded The Left / Gysi, correct is CMP:
dt$CMPid.5[dt$Es=="DEU_1994"]=41221#

dt$CMPid.1[dt$Es=="DEU_1998"]=41320
dt$CMPid.2[dt$Es=="DEU_1998"]=41521
dt$CMPid.3[dt$Es=="DEU_1998"]=41113
dt$CMPid.4[dt$Es=="DEU_1998"]=41420
#CMP coded Party of Democratic Socialism while TEV coded The Left / Gysi, correct is CMP:
dt$CMPid.5[dt$Es=="DEU_1998"]=41221#

dt$CMPid.1[dt$Es=="DEU_2009"]=41521
dt$CMPid.2[dt$Es=="DEU_2009"]=41320
dt$CMPid.3[dt$Es=="DEU_2009"]=41420
dt$CMPid.4[dt$Es=="DEU_2009"]=41223
dt$CMPid.5[dt$Es=="DEU_2009"]=41113

dt$CMPid.1[dt$Es=="DEU_2013"]=41521
dt$CMPid.2[dt$Es=="DEU_2013"]=41320
dt$CMPid.3[dt$Es=="DEU_2013"]=41223
dt$CMPid.4[dt$Es=="DEU_2013"]=41113
dt$CMPid.5[dt$Es=="DEU_2013"]=41420

dt$CMPid.1[dt$Es=="FIN_2007"]=14810
dt$CMPid.2[dt$Es=="FIN_2007"]=14620
dt$CMPid.3[dt$Es=="FIN_2007"]=14320
dt$CMPid.4[dt$Es=="FIN_2007"]=14223
dt$CMPid.5[dt$Es=="FIN_2007"]=14110
dt$CMPid.6[dt$Es=="FIN_2007"]=14520

dt$CMPid.1[dt$Es=="FIN_2011"]=14620
dt$CMPid.2[dt$Es=="FIN_2011"]=14320
dt$CMPid.3[dt$Es=="FIN_2011"]=14820
dt$CMPid.4[dt$Es=="FIN_2011"]=14810
dt$CMPid.5[dt$Es=="FIN_2011"]=14223
dt$CMPid.6[dt$Es=="FIN_2011"]=14110

dt$CMPid.1[dt$Es=="GRC_2009"]=34313
dt$CMPid.2[dt$Es=="GRC_2009"]=34511
dt$CMPid.3[dt$Es=="GRC_2009"]=34210
dt$CMPid.4[dt$Es=="GRC_2009"]=34710
dt$CMPid.5[dt$Es=="GRC_2009"]=34020
dt$CMPid.6[dt$Es=="GRC_2009"]=99990

dt$CMPid.1[dt$Es=="GRC_2012"]=34511
dt$CMPid.2[dt$Es=="GRC_2012"]=34212
dt$CMPid.3[dt$Es=="GRC_2012"]=34313
dt$CMPid.4[dt$Es=="GRC_2012"]=34730
dt$CMPid.5[dt$Es=="GRC_2012"]=34720
dt$CMPid.6[dt$Es=="GRC_2012"]=34213

dt$CMPid.1[dt$Es=="IRL_2007"]=53620
dt$CMPid.2[dt$Es=="IRL_2007"]=53520
dt$CMPid.3[dt$Es=="IRL_2007"]=53320
dt$CMPid.4[dt$Es=="IRL_2007"]=53110
dt$CMPid.5[dt$Es=="IRL_2007"]=53951
dt$CMPid.6[dt$Es=="IRL_2007"]=53420

dt$CMPid.1[dt$Es=="IRL_2011"]=53520
dt$CMPid.2[dt$Es=="IRL_2011"]=53320
dt$CMPid.3[dt$Es=="IRL_2011"]=53620
dt$CMPid.4[dt$Es=="IRL_2011"]=53951
dt$CMPid.5[dt$Es=="IRL_2011"]=53110#

dt$CMPid.1[dt$Es=="ISL_1999"]=15620
dt$CMPid.2[dt$Es=="ISL_1999"]=15328
dt$CMPid.3[dt$Es=="ISL_1999"]=15810
dt$CMPid.4[dt$Es=="ISL_1999"]=15111
dt$CMPid.5[dt$Es=="ISL_1999"]=15420

dt$CMPid.1[dt$Es=="ISL_2007"]=15620
dt$CMPid.2[dt$Es=="ISL_2007"]=15328
dt$CMPid.3[dt$Es=="ISL_2007"]=15111
dt$CMPid.4[dt$Es=="ISL_2007"]=15810
dt$CMPid.5[dt$Es=="ISL_2007"]=15420
dt$CMPid.6[dt$Es=="ISL_2007"]=99990#

dt$CMPid.1[dt$Es=="ISL_2009"]=15328
dt$CMPid.2[dt$Es=="ISL_2009"]=15620
dt$CMPid.3[dt$Es=="ISL_2009"]=15111
dt$CMPid.4[dt$Es=="ISL_2009"]=15810
dt$CMPid.5[dt$Es=="ISL_2009"]=15420#
dt$CMPid.6[dt$Es=="ISL_2009"]=15430#

dt$CMPid.1[dt$Es=="ISL_2013"]=15620
dt$CMPid.2[dt$Es=="ISL_2013"]=15810
dt$CMPid.3[dt$Es=="ISL_2013"]=15328
dt$CMPid.4[dt$Es=="ISL_2013"]=15111
dt$CMPid.5[dt$Es=="ISL_2013"]=15440
dt$CMPid.6[dt$Es=="ISL_2013"]=15952

dt$CMPid.1[dt$Es=="NLD_1994"]=22320
dt$CMPid.2[dt$Es=="NLD_1994"]=22521
dt$CMPid.3[dt$Es=="NLD_1994"]=22420
dt$CMPid.4[dt$Es=="NLD_1994"]=22330

dt$CMPid.1[dt$Es=="NLD_1998"]=22320#EV 4 parties, CSES 6 parties coded
dt$CMPid.2[dt$Es=="NLD_1998"]=22420
dt$CMPid.3[dt$Es=="NLD_1998"]=22521
dt$CMPid.4[dt$Es=="NLD_1998"]=22330
dt$CMPid.5[dt$Es=="NLD_1998"]=22110
dt$CMPid.6[dt$Es=="NLD_1998"]=22527#

dt$CMPid.1[dt$Es=="NOR_1981"]=12320
dt$CMPid.2[dt$Es=="NOR_1981"]=12620
dt$CMPid.3[dt$Es=="NOR_1981"]=12520
dt$CMPid.4[dt$Es=="NOR_1981"]=12810
dt$CMPid.5[dt$Es=="NOR_1981"]=12221
dt$CMPid.6[dt$Es=="NOR_1981"]=12951

dt$CMPid.1[dt$Es=="NOR_1985"]=12320
dt$CMPid.2[dt$Es=="NOR_1985"]=12620
dt$CMPid.3[dt$Es=="NOR_1985"]=12520
dt$CMPid.4[dt$Es=="NOR_1985"]=12810
dt$CMPid.5[dt$Es=="NOR_1985"]=12221
dt$CMPid.6[dt$Es=="NOR_1985"]=12951

dt$CMPid.1[dt$Es=="NOR_1989"]=12320
dt$CMPid.2[dt$Es=="NOR_1989"]=12620
dt$CMPid.3[dt$Es=="NOR_1989"]=12951
dt$CMPid.4[dt$Es=="NOR_1989"]=12221
dt$CMPid.5[dt$Es=="NOR_1989"]=12520
dt$CMPid.6[dt$Es=="NOR_1989"]=12810

dt$CMPid.1[dt$Es=="NOR_1993"]=12320
dt$CMPid.2[dt$Es=="NOR_1993"]=12810
dt$CMPid.3[dt$Es=="NOR_1993"]=12620
dt$CMPid.4[dt$Es=="NOR_1993"]=12221
dt$CMPid.5[dt$Es=="NOR_1993"]=12520
dt$CMPid.6[dt$Es=="NOR_1993"]=12951

dt$CMPid.1[dt$Es=="NOR_1997"]=12320
dt$CMPid.2[dt$Es=="NOR_1997"]=12951
dt$CMPid.3[dt$Es=="NOR_1997"]=12520
dt$CMPid.4[dt$Es=="NOR_1997"]=12620
dt$CMPid.5[dt$Es=="NOR_1997"]=12810
dt$CMPid.6[dt$Es=="NOR_1997"]=12221

dt$CMPid.1[dt$Es=="NOR_2005"]=12320
dt$CMPid.2[dt$Es=="NOR_2005"]=12951
dt$CMPid.3[dt$Es=="NOR_2005"]=12620
dt$CMPid.4[dt$Es=="NOR_2005"]=12221
dt$CMPid.5[dt$Es=="NOR_2005"]=12520
dt$CMPid.6[dt$Es=="NOR_2005"]=12810

dt$CMPid.1[dt$Es=="PRT_2002"]=35313
dt$CMPid.2[dt$Es=="PRT_2002"]=35311
dt$CMPid.3[dt$Es=="PRT_2002"]=35520
dt$CMPid.4[dt$Es=="PRT_2002"]=35220#CMP codes the communist party instead than the coalition
dt$CMPid.5[dt$Es=="PRT_2002"]=35211
dt$CMPid.6[dt$Es=="PRT_2002"]=99990

dt$CMPid.1[dt$Es=="PRT_2009"]=35311#osym4-5  inverted!?
dt$CMPid.2[dt$Es=="PRT_2009"]=35313
dt$CMPid.3[dt$Es=="PRT_2009"]=35520
dt$CMPid.4[dt$Es=="PRT_2009"]=35211
dt$CMPid.5[dt$Es=="PRT_2009"]=35220#CMP codes the communist party instead than the coalition

dt$CMPid.1[dt$Es=="SWE_1982"]=11320
dt$CMPid.2[dt$Es=="SWE_1982"]=11620
dt$CMPid.3[dt$Es=="SWE_1982"]=11810
dt$CMPid.4[dt$Es=="SWE_1982"]=11420
dt$CMPid.5[dt$Es=="SWE_1982"]=11220

dt$CMPid.1[dt$Es=="SWE_1985"]=11320
dt$CMPid.2[dt$Es=="SWE_1985"]=11620
dt$CMPid.3[dt$Es=="SWE_1985"]=11420
dt$CMPid.4[dt$Es=="SWE_1985"]=11810
dt$CMPid.5[dt$Es=="SWE_1985"]=11220
dt$CMPid.6[dt$Es=="SWE_1985"]=11520

dt$CMPid.1[dt$Es=="SWE_1988"]=11320
dt$CMPid.2[dt$Es=="SWE_1988"]=11620
dt$CMPid.3[dt$Es=="SWE_1988"]=11420
dt$CMPid.4[dt$Es=="SWE_1988"]=11810
dt$CMPid.5[dt$Es=="SWE_1988"]=11220

dt$CMPid.1[dt$Es=="SWE_1991"]=11320
dt$CMPid.2[dt$Es=="SWE_1991"]=11620
dt$CMPid.3[dt$Es=="SWE_1991"]=11420
dt$CMPid.4[dt$Es=="SWE_1991"]=11810
dt$CMPid.5[dt$Es=="SWE_1991"]=11520
dt$CMPid.6[dt$Es=="SWE_1991"]=11951

dt$CMPid.1[dt$Es=="SWE_1994"]=11320
dt$CMPid.2[dt$Es=="SWE_1994"]=11620
dt$CMPid.3[dt$Es=="SWE_1994"]=11810
dt$CMPid.4[dt$Es=="SWE_1994"]=11420
dt$CMPid.5[dt$Es=="SWE_1994"]=11220
dt$CMPid.6[dt$Es=="SWE_1994"]=11110

dt$CMPid.1[dt$Es=="SWE_1998"]=11320
dt$CMPid.2[dt$Es=="SWE_1998"]=11620
dt$CMPid.3[dt$Es=="SWE_1998"]=11220
dt$CMPid.4[dt$Es=="SWE_1998"]=11520
dt$CMPid.5[dt$Es=="SWE_1998"]=11810
dt$CMPid.6[dt$Es=="SWE_1998"]=11420

dt$CMPid.1[dt$Es=="SWE_2006"]=11320
dt$CMPid.2[dt$Es=="SWE_2006"]=11620
dt$CMPid.3[dt$Es=="SWE_2006"]=11810
dt$CMPid.4[dt$Es=="SWE_2006"]=11420
dt$CMPid.5[dt$Es=="SWE_2006"]=11520
dt$CMPid.6[dt$Es=="SWE_2006"]=11220

dt$CMPid.1=as.numeric(dt$CMPid.1)#recode as numeric:
dt$CMPid.2=as.numeric(dt$CMPid.2)
dt$CMPid.3=as.numeric(dt$CMPid.3)
dt$CMPid.4=as.numeric(dt$CMPid.4)
dt$CMPid.5=as.numeric(dt$CMPid.5)
dt$CMPid.6=as.numeric(dt$CMPid.6)


#Pv.i (pervote):----

#NB: TEV coded pervote in the "stacked" dataset, but it is impossible to recover that data,
# thus I recode pervote from the CMP dataset via CMPid, country, year.

#read CMP dataset:
dCMP=read.xlsx("MPDataset_MPDS2020b.xlsx")

#filter CMP dataset and set variable names:
colnames(dCMP)[colnames(dCMP)=="party"]="CMPid_temp"
colnames(dCMP)[colnames(dCMP)=="pervote"]="Pv"

dCMP$Ec=NA
dCMP$Ec=as.character(dCMP$Ec)
dCMP$Ec[dCMP$country==63]="AUS"
dCMP$Ec[dCMP$country==42]="AUT"
dCMP$Ec[dCMP$country==62]="CAN"
dCMP$Ec[dCMP$country==41]="DEU"
dCMP$Ec[dCMP$country==13]="DNK"
dCMP$Ec[dCMP$country==33]="ESP"
dCMP$Ec[dCMP$country==14]="FIN"
dCMP$Ec[dCMP$country==51]="GBR"
dCMP$Ec[dCMP$country==34]="GRC"
dCMP$Ec[dCMP$country==53]="IRL"
dCMP$Ec[dCMP$country==15]="ISL"
dCMP$Ec[dCMP$country==72]="ISR"
dCMP$Ec[dCMP$country==32]="ITA"
dCMP$Ec[dCMP$country==22]="NLD"
dCMP$Ec[dCMP$country==12]="NOR"
dCMP$Ec[dCMP$country==64]="NZL"
dCMP$Ec[dCMP$country==35]="PRT"
dCMP$Ec[dCMP$country==11]="SWE"
dCMP$Ec[dCMP$country==83]="EST"#
dCMP$Ec[dCMP$country==86]="HUN"
dCMP$Ec[dCMP$country==88]="LTU"
dCMP$Ec[dCMP$country==97]="SVN"

dCMP$Ey=as.numeric(substr(dCMP$date,1,4))

#GRC_2012: there was an election one month before in may, need to exclude it from dCMP
# to merge in dt only the parties in the June election (which is the one coded by TEV):
dCMP=dCMP[!(dCMP$Ec=="GRC"&dCMP$date==201205),]
#same for GRC_1989: I excluded the June election because there are less than three parties coded,
# and need to exclude it from DCMP:
dCMP=dCMP[!(dCMP$Ec=="GRC"&dCMP$date==198906),]

#keep only variables of interest:
dCMP=dCMP[,c(175,176,7,16)]

#code pervote alternative i (Pv.i):
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.1"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T, all.y=F)
colnames(dt)[colnames(dt)=="Pv"]="Pv.1"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.2"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="Pv"]="Pv.2"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.3"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="Pv"]="Pv.3"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.4"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="Pv"]="Pv.4"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.5"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="Pv"]="Pv.5"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.6"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="Pv"]="Pv.6"

#recode Pv.i not coded in CMP:
#TEV coded Icelandic Movement (IM) / Ómar Ragnarsson but CMP did not assign vote:
dt$Pv.6[dt$Es=="ISL_2007"]=5.95
#TEV coded Liberal Party (LP) / Guðjón Arnar Kristjánsson but CMP did not assign vote:
dt$Pv.5[dt$Es=="ISL_2009"]=4.14
#TEV coded Communist Party of Greece (interior) KKEes / Kyrkos but CMP did not assign vote:
dt$Pv.4[dt$Es=="GRC_1985"]=0.1
#TEV coded Ecologists-Greens (OP) / Xrisogelos but CMP did not assign vote:
dt$Pv.6[dt$Es=="GRC_2009"]=2.53
#TEV coded The Left – The rainbow (SA) / Fausto Bertinotti but CMP did not assign vote:
dt$Pv.6[dt$Es=="ITA_2008"]=3.08
#TEV coded Portuguese Workers' Communist Party (PCTP/MRPP) / Garcia Pereira but CMP did not assign vote:
dt$Pv.6[dt$Es=="PRT_2002"]=0.66


#Ps.i (perseats):----

#read CMP dataset:
dCMP=read.xlsx("MPDataset_MPDS2020b.xlsx")

#filter CMP dataset and set variable names:
colnames(dCMP)[colnames(dCMP)=="party"]="CMPid_temp"
colnames(dCMP)[colnames(dCMP)=="absseat"]="Ps"
colnames(dCMP)[colnames(dCMP)=="totseats"]="Pst"

dCMP$Ec=NA
dCMP$Ec=as.character(dCMP$Ec)
dCMP$Ec[dCMP$country==63]="AUS"
dCMP$Ec[dCMP$country==42]="AUT"
dCMP$Ec[dCMP$country==62]="CAN"
dCMP$Ec[dCMP$country==41]="DEU"
dCMP$Ec[dCMP$country==13]="DNK"
dCMP$Ec[dCMP$country==33]="ESP"
dCMP$Ec[dCMP$country==14]="FIN"
dCMP$Ec[dCMP$country==51]="GBR"
dCMP$Ec[dCMP$country==34]="GRC"
dCMP$Ec[dCMP$country==53]="IRL"
dCMP$Ec[dCMP$country==15]="ISL"
dCMP$Ec[dCMP$country==72]="ISR"
dCMP$Ec[dCMP$country==32]="ITA"
dCMP$Ec[dCMP$country==22]="NLD"
dCMP$Ec[dCMP$country==12]="NOR"
dCMP$Ec[dCMP$country==64]="NZL"
dCMP$Ec[dCMP$country==35]="PRT"
dCMP$Ec[dCMP$country==11]="SWE"
dCMP$Ec[dCMP$country==83]="EST"#
dCMP$Ec[dCMP$country==86]="HUN"
dCMP$Ec[dCMP$country==88]="LTU"
dCMP$Ec[dCMP$country==97]="SVN"

dCMP$Ey=as.numeric(substr(dCMP$date,1,4))

#GRC_2012: there was an election one month before in may, need to exclude it from dCMP
# to merge in dt only the parties in the june election (which is the one coded by TEV):
dCMP=dCMP[!(dCMP$Ec=="GRC"&dCMP$date==201205),]
#same for GRC_1989: I excluded the June election because there are less than three parties coded,
# and need to exclude it from DCMP:
dCMP=dCMP[!(dCMP$Ec=="GRC"&dCMP$date==198906),]

#code total seats:
dCMP_temp=dCMP[,c(175,176,7,20)]
dt=merge(dt, dCMP_temp, by.x=c("Ec","Ey","CMPid.1"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
# #check:
# summary(dt$CMPid.1)
# summary(dt$Pst)
# sort(unique(dt$Es[is.na(dt$Pst)]))
rm(dCMP_temp)

#keep only variables of interest:
dCMP=dCMP[,c(175,176,7,19)]

#code pervote alternative i (Ps.i):
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.1"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="Ps"]="Ps.1"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.2"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="Ps"]="Ps.2"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.3"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="Ps"]="Ps.3"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.4"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="Ps"]="Ps.4"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.5"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="Ps"]="Ps.5"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.6"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="Ps"]="Ps.6"

#recode Ps.i not coded in CMP:
#TEV coded Icelandic Movement (IM) / Ómar Ragnarsson but CMP did not assign seats:
dt$Ps.6[dt$Es=="ISL_2007"]=0
#TEV coded Liberal Party (LP) / Guðjón Arnar Kristjánsson but CMP did not assign seats:
dt$Ps.5[dt$Es=="ISL_2009"]=0
#TEV coded Communist Party of Greece (interior) KKEes / Kyrkos but CMP did not assign seats:
dt$Ps.4[dt$Es=="GRC_1985"]=1
#TEV coded Ecologists-Greens (OP) / Xrisogelos but CMP did not assign seats:
dt$Ps.6[dt$Es=="GRC_2009"]=0
#TEV coded The Left – The rainbow (SA) / Fausto Bertinotti but CMP did not assign seats:
dt$Ps.6[dt$Es=="ITA_2008"]=0
#TEV coded Portuguese Workers' Communist Party (PCTP/MRPP) / Garcia Pereira but CMP did not assign seats:
dt$Ps.6[dt$Es=="PRT_2002"]=0


#get seat share %:
dt$Ps.1=dt$Ps.1/dt$Pst*100
dt$Ps.2=dt$Ps.2/dt$Pst*100
dt$Ps.3=dt$Ps.3/dt$Pst*100
dt$Ps.4=dt$Ps.4/dt$Pst*100
dt$Ps.5=dt$Ps.5/dt$Pst*100
dt$Ps.6=dt$Ps.6/dt$Pst*100


#Seats to vote ratios:----
dt$P1SVrat=dt$Ps.1/dt$Pv.1#for most voted party
dt$P2SVrat=dt$Ps.2/dt$Pv.2#for second most voted party
dt$P1P2rat=dt$P1SVrat/dt$P2SVrat#ratio of rations first/second party
summary(dt$P1P2rat)


#Gallagher Disproportionality measure:----
#(coded only for the elections that will remain in the final dataset)
sort(unique(dt$Es))

dt$GDM=NA
dt$GDM[dt$Es=="DEU_1961"]=3.43#
dt$GDM[dt$Es=="DEU_1965"]=2.31#
dt$GDM[dt$Es=="DEU_1976"]=0.59#
dt$GDM[dt$Es=="DEU_1980"]=1.41#
dt$GDM[dt$Es=="DEU_1983"]=0.50#
dt$GDM[dt$Es=="DEU_1987"]=0.76#
dt$GDM[dt$Es=="DEU_1990"]=4.63#
dt$GDM[dt$Es=="DEU_1994"]=2.22#
dt$GDM[dt$Es=="DEU_1998"]=3.15#The figure in the effective number of legislative parties (Ns) column is based on 
dt$GDM[dt$Es=="DEU_2009"]=3.40#treating the CDU and CSU as separate parties.
dt$GDM[dt$Es=="DEU_2013"]=7.83#
dt$GDM[dt$Es=="ESP_1979"]=10.56
dt$GDM[dt$Es=="ESP_1982"]=8.02
dt$GDM[dt$Es=="ESP_1993"]=7.08
dt$GDM[dt$Es=="ESP_2004"]=4.25
dt$GDM[dt$Es=="FIN_2003"]=3.16
dt$GDM[dt$Es=="FIN_2007"]=3.20
dt$GDM[dt$Es=="FIN_2011"]=2.95
dt$GDM[dt$Es=="GBR_2005"]=16.73
dt$GDM[dt$Es=="GRC_1985"]=7.08
dt$GDM[dt$Es=="GRC_1989"]=3.94
dt$GDM[dt$Es=="GRC_1990"]=3.97
dt$GDM[dt$Es=="GRC_1996"]=9.45
dt$GDM[dt$Es=="GRC_2000"]=6.78
dt$GDM[dt$Es=="GRC_2004"]=7.37
dt$GDM[dt$Es=="GRC_2007"]=6.99
dt$GDM[dt$Es=="GRC_2009"]=7.29
dt$GDM[dt$Es=="GRC_2012"]=9.96#Jun
dt$GDM[dt$Es=="IRL_2002"]=6.62
dt$GDM[dt$Es=="IRL_2011"]=8.69
dt$GDM[dt$Es=="ISL_1987"]=2.31
dt$GDM[dt$Es=="ISL_1991"]=2.79
dt$GDM[dt$Es=="ISL_1995"]=1.98
dt$GDM[dt$Es=="ISL_1999"]=1.06
dt$GDM[dt$Es=="ISL_2003"]=1.85
dt$GDM[dt$Es=="ISL_2007"]=3.49
dt$GDM[dt$Es=="ISL_2009"]=2.58
dt$GDM[dt$Es=="ISL_2013"]=6.23
dt$GDM[dt$Es=="ITA_2006"]=3.61
dt$GDM[dt$Es=="ITA_2008"]=5.73
dt$GDM[dt$Es=="NLD_1994"]=1.08
dt$GDM[dt$Es=="NLD_1998"]=1.28
dt$GDM[dt$Es=="NLD_2002"]=0.88
dt$GDM[dt$Es=="NOR_1981"]=4.94
dt$GDM[dt$Es=="NOR_1985"]=4.75
dt$GDM[dt$Es=="NOR_1989"]=3.67
dt$GDM[dt$Es=="NOR_1993"]=3.95
dt$GDM[dt$Es=="NOR_1997"]=3.44
dt$GDM[dt$Es=="NOR_2005"]=2.67
dt$GDM[dt$Es=="PRT_2002"]=4.64
dt$GDM[dt$Es=="PRT_2005"]=5.75
dt$GDM[dt$Es=="PRT_2009"]=5.63
dt$GDM[dt$Es=="SWE_1979"]=1.27
dt$GDM[dt$Es=="SWE_1982"]=2.40
dt$GDM[dt$Es=="SWE_1985"]=1.35
dt$GDM[dt$Es=="SWE_1988"]=2.45
dt$GDM[dt$Es=="SWE_1991"]=2.86
dt$GDM[dt$Es=="SWE_1994"]=1.18
dt$GDM[dt$Es=="SWE_1998"]=0.97
dt$GDM[dt$Es=="SWE_2002"]=1.52
dt$GDM[dt$Es=="SWE_2006"]=3.02
dt$GDM[dt$Es=="SWE_2010"]=1.25


#CMPrile:----

#read CMP dataset:
dCMP=read.xlsx("MPDataset_MPDS2020b.xlsx")

#filter CMP dataset and set variable names:
colnames(dCMP)[colnames(dCMP)=="party"]="CMPid_temp"
colnames(dCMP)[colnames(dCMP)=="rile"]="CMPrile"

dCMP$Ec=NA
dCMP$Ec=as.character(dCMP$Ec)
dCMP$Ec[dCMP$country==63]="AUS"
dCMP$Ec[dCMP$country==42]="AUT"
dCMP$Ec[dCMP$country==62]="CAN"
dCMP$Ec[dCMP$country==41]="DEU"
dCMP$Ec[dCMP$country==13]="DNK"
dCMP$Ec[dCMP$country==33]="ESP"
dCMP$Ec[dCMP$country==14]="FIN"
dCMP$Ec[dCMP$country==51]="GBR"
dCMP$Ec[dCMP$country==34]="GRC"
dCMP$Ec[dCMP$country==53]="IRL"
dCMP$Ec[dCMP$country==15]="ISL"
dCMP$Ec[dCMP$country==72]="ISR"
dCMP$Ec[dCMP$country==32]="ITA"
dCMP$Ec[dCMP$country==22]="NLD"
dCMP$Ec[dCMP$country==12]="NOR"
dCMP$Ec[dCMP$country==64]="NZL"
dCMP$Ec[dCMP$country==35]="PRT"
dCMP$Ec[dCMP$country==11]="SWE"
dCMP$Ec[dCMP$country==83]="EST"#
dCMP$Ec[dCMP$country==86]="HUN"
dCMP$Ec[dCMP$country==88]="LTU"
dCMP$Ec[dCMP$country==97]="SVN"

#GRC_2012: there was an election one month before in May, need to exclude it from dCMP
# to merge in dt only the parties in the June election (which is the one coded by TEV):
dCMP=dCMP[!(dCMP$Ec=="GRC"&dCMP$date==201205),]
#same for GRC_1989: I excluded the June election because there are less than three parties coded,
# and need to exclude it from DCMP:
dCMP=dCMP[!(dCMP$Ec=="GRC"&dCMP$date==198906),]

dCMP$Ey=as.numeric(substr(dCMP$date,1,4))

#keep only variables of interest:
dCMP=dCMP[,c(175,176,7,168)]

#code pervote alternative i (CMPrile.i):
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.1"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="CMPrile"]="CMPrile.1"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.2"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="CMPrile"]="CMPrile.2"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.3"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="CMPrile"]="CMPrile.3"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.4"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="CMPrile"]="CMPrile.4"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.5"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="CMPrile"]="CMPrile.5"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.6"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="CMPrile"]="CMPrile.6"
rm(dCMP)
# #check:
# #here the missing values cannot be replaced since there is not extra-CMP available rile


#CMPper415:----

#read CMP dataset:
dCMP=read.xlsx("MPDataset_MPDS2020b.xlsx")

#filter CMP dataset and set variable names:
colnames(dCMP)[colnames(dCMP)=="party"]="CMPid_temp"
colnames(dCMP)[colnames(dCMP)=="per415"]="CMPper415"

dCMP$Ec=NA
dCMP$Ec=as.character(dCMP$Ec)
dCMP$Ec[dCMP$country==63]="AUS"
dCMP$Ec[dCMP$country==42]="AUT"
dCMP$Ec[dCMP$country==62]="CAN"
dCMP$Ec[dCMP$country==41]="DEU"
dCMP$Ec[dCMP$country==13]="DNK"
dCMP$Ec[dCMP$country==33]="ESP"
dCMP$Ec[dCMP$country==14]="FIN"
dCMP$Ec[dCMP$country==51]="GBR"
dCMP$Ec[dCMP$country==34]="GRC"
dCMP$Ec[dCMP$country==53]="IRL"
dCMP$Ec[dCMP$country==15]="ISL"
dCMP$Ec[dCMP$country==72]="ISR"
dCMP$Ec[dCMP$country==32]="ITA"
dCMP$Ec[dCMP$country==22]="NLD"
dCMP$Ec[dCMP$country==12]="NOR"
dCMP$Ec[dCMP$country==64]="NZL"
dCMP$Ec[dCMP$country==35]="PRT"
dCMP$Ec[dCMP$country==11]="SWE"
dCMP$Ec[dCMP$country==83]="EST"#
dCMP$Ec[dCMP$country==86]="HUN"
dCMP$Ec[dCMP$country==88]="LTU"
dCMP$Ec[dCMP$country==97]="SVN"

#GRC_2012: there was an election one month before in May, need to exclude it from dCMP
# to merge in dt only the parties in the June election (which is the one coded by TEV):
dCMP=dCMP[!(dCMP$Ec=="GRC"&dCMP$date==201205),]
#same for GRC_1989: I excluded the June election because there are less than three parties coded,
# and need to exclude it from DCMP:
dCMP=dCMP[!(dCMP$Ec=="GRC"&dCMP$date==198906),]

dCMP$Ey=as.numeric(substr(dCMP$date,1,4))

#keep only variables of interest:
dCMP=dCMP[,c(175,176,7,59)]

#code pervote alternative i (CMPper415.i):
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.1"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="CMPper415"]="CMPper415.1"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.2"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="CMPper415"]="CMPper415.2"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.3"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="CMPper415"]="CMPper415.3"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.4"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="CMPper415"]="CMPper415.4"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.5"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="CMPper415"]="CMPper415.5"
dt=merge(dt, dCMP, by.x=c("Ec","Ey","CMPid.6"), by.y=c("Ec","Ey","CMPid_temp"), all.x=T)
colnames(dt)[colnames(dt)=="CMPper415"]="CMPper415.6"
rm(dCMP)
# #check:
# #here the missing values cannot be replaced since there is not extra-CMP available rile


#Pi (party identification):----
# table(dt$RPID1)
# # 1 Does not identify with a political party
# # 2     Does identify with a political party
# table(dt$RPID4)
# # The direction of the party identification: which party? See Appendix 10 for party codes

dt$Pi=NA

dt$Pi[dt$Es=="ESP_1979"&dt$RPID4==1]=1
dt$Pi[dt$Es=="ESP_1979"&dt$RPID4==2]=2
dt$Pi[dt$Es=="ESP_1979"&dt$RPID4==3]=3
dt$Pi[dt$Es=="ESP_1979"&dt$RPID4==4]=4

dt$Pi[dt$Es=="ESP_1982"&dt$RPID4==3]=1
dt$Pi[dt$Es=="ESP_1982"&dt$RPID4==2]=2
dt$Pi[dt$Es=="ESP_1982"&dt$RPID4==4]=3
dt$Pi[dt$Es=="ESP_1982"&dt$RPID4==1]=4
dt$Pi[dt$Es=="ESP_1982"&dt$RPID4==5]=5

dt$Pi[dt$Es=="ESP_1993"&dt$RPID4==1]=1
dt$Pi[dt$Es=="ESP_1993"&dt$RPID4==3]=2
dt$Pi[dt$Es=="ESP_1993"&dt$RPID4==4]=3
dt$Pi[dt$Es=="ESP_1993"&dt$RPID4==2]=4

dt$Pi[dt$Es=="ESP_2004"&dt$RPID4==1]=1
dt$Pi[dt$Es=="ESP_2004"&dt$RPID4==2]=2
dt$Pi[dt$Es=="ESP_2004"&dt$RPID4==3]=3

dt$Pi[dt$Es=="EST_2011"&dt$RPID4==1]=1
dt$Pi[dt$Es=="EST_2011"&dt$RPID4==2]=2
dt$Pi[dt$Es=="EST_2011"&dt$RPID4==3]=3
dt$Pi[dt$Es=="EST_2011"&dt$RPID4==4]=4
dt$Pi[dt$Es=="EST_2011"&dt$RPID4==6]=5
dt$Pi[dt$Es=="EST_2011"&dt$RPID4==5]=6

dt$Pi[dt$Es=="FIN_2003"&dt$RPID4==1]=1
dt$Pi[dt$Es=="FIN_2003"&dt$RPID4==3]=2
dt$Pi[dt$Es=="FIN_2003"&dt$RPID4==2]=3
dt$Pi[dt$Es=="FIN_2003"&dt$RPID4==4]=4
dt$Pi[dt$Es=="FIN_2003"&dt$RPID4==5]=5
dt$Pi[dt$Es=="FIN_2003"&dt$RPID4==6]=6

dt$Pi[dt$Es=="GBR_2005"&dt$RPID4==1]=1
dt$Pi[dt$Es=="GBR_2005"&dt$RPID4==2]=2
dt$Pi[dt$Es=="GBR_2005"&dt$RPID4==3]=3

dt$Pi[dt$Es=="GRC_1985"&dt$RPID4==1]=1
dt$Pi[dt$Es=="GRC_1985"&dt$RPID4==2]=2
dt$Pi[dt$Es=="GRC_1985"&dt$RPID4==3]=3
dt$Pi[dt$Es=="GRC_1985"&dt$RPID4==4]=4

dt$Pi[dt$Es=="GRC_1989"&dt$RPID4==1]=1
dt$Pi[dt$Es=="GRC_1989"&dt$RPID4==2]=2
dt$Pi[dt$Es=="GRC_1989"&dt$RPID4==3]=3

dt$Pi[dt$Es=="GRC_1990"&dt$RPID4==1]=1
dt$Pi[dt$Es=="GRC_1990"&dt$RPID4==2]=2
dt$Pi[dt$Es=="GRC_1990"&dt$RPID4==3]=3

dt$Pi[dt$Es=="GRC_1996"&dt$RPID4==1]=1
dt$Pi[dt$Es=="GRC_1996"&dt$RPID4==2]=2
dt$Pi[dt$Es=="GRC_1996"&dt$RPID4==3]=3
dt$Pi[dt$Es=="GRC_1996"&dt$RPID4==4]=4
dt$Pi[dt$Es=="GRC_1996"&dt$RPID4==5]=5

dt$Pi[dt$Es=="GRC_2000"&dt$RPID4==1]=1
dt$Pi[dt$Es=="GRC_2000"&dt$RPID4==2]=2
dt$Pi[dt$Es=="GRC_2000"&dt$RPID4==3]=3
dt$Pi[dt$Es=="GRC_2000"&dt$RPID4==4]=4

dt$Pi[dt$Es=="GRC_2004"&dt$RPID4==1]=1
dt$Pi[dt$Es=="GRC_2004"&dt$RPID4==2]=2
dt$Pi[dt$Es=="GRC_2004"&dt$RPID4==3]=3
dt$Pi[dt$Es=="GRC_2004"&dt$RPID4==4]=4

dt$Pi[dt$Es=="GRC_2007"&dt$RPID4==1]=1
dt$Pi[dt$Es=="GRC_2007"&dt$RPID4==2]=2
dt$Pi[dt$Es=="GRC_2007"&dt$RPID4==3]=3
dt$Pi[dt$Es=="GRC_2007"&dt$RPID4==4]=4
dt$Pi[dt$Es=="GRC_2007"&dt$RPID4==5]=5

dt$Pi[dt$Es=="HUN_1998"&dt$RPID4==1]=1
dt$Pi[dt$Es=="HUN_1998"&dt$RPID4==5]=2
dt$Pi[dt$Es=="HUN_1998"&dt$RPID4==2]=3
dt$Pi[dt$Es=="HUN_1998"&dt$RPID4==6]=4
dt$Pi[dt$Es=="HUN_1998"&dt$RPID4==4]=5
dt$Pi[dt$Es=="HUN_1998"&dt$RPID4==3]=6

dt$Pi[dt$Es=="HUN_2006"&dt$RPID4==1]=1
dt$Pi[dt$Es=="HUN_2006"&dt$RPID4==2]=2
dt$Pi[dt$Es=="HUN_2006"&dt$RPID4==3]=3
dt$Pi[dt$Es=="HUN_2006"&dt$RPID4==4]=4

dt$Pi[dt$Es=="IRL_2002"&dt$RPID4==1]=1
dt$Pi[dt$Es=="IRL_2002"&dt$RPID4==2]=2
dt$Pi[dt$Es=="IRL_2002"&dt$RPID4==4]=3
dt$Pi[dt$Es=="IRL_2002"&dt$RPID4==5]=4
dt$Pi[dt$Es=="IRL_2002"&dt$RPID4==3]=5
dt$Pi[dt$Es=="IRL_2002"&dt$RPID4==6]=6

dt$Pi[dt$Es=="ISL_1987"&dt$RPID4==3]=1
dt$Pi[dt$Es=="ISL_1987"&dt$RPID4==2]=2
dt$Pi[dt$Es=="ISL_1987"&dt$RPID4==1]=3
dt$Pi[dt$Es=="ISL_1987"&dt$RPID4==4]=4
dt$Pi[dt$Es=="ISL_1987"&dt$RPID4==5]=5

dt$Pi[dt$Es=="ISL_1991"&dt$RPID4==3]=1
dt$Pi[dt$Es=="ISL_1991"&dt$RPID4==2]=2
dt$Pi[dt$Es=="ISL_1991"&dt$RPID4==1]=3
dt$Pi[dt$Es=="ISL_1991"&dt$RPID4==4]=4
dt$Pi[dt$Es=="ISL_1991"&dt$RPID4==5]=5

dt$Pi[dt$Es=="ISL_1995"&dt$RPID4==3]=1
dt$Pi[dt$Es=="ISL_1995"&dt$RPID4==2]=2
dt$Pi[dt$Es=="ISL_1995"&dt$RPID4==4]=3
dt$Pi[dt$Es=="ISL_1995"&dt$RPID4==1]=4
dt$Pi[dt$Es=="ISL_1995"&dt$RPID4==5]=5
dt$Pi[dt$Es=="ISL_1995"&dt$RPID4==6]=6

dt$Pi[dt$Es=="ISL_2003"&dt$RPID4==3]=1
dt$Pi[dt$Es=="ISL_2003"&dt$RPID4==1]=2
dt$Pi[dt$Es=="ISL_2003"&dt$RPID4==2]=3
dt$Pi[dt$Es=="ISL_2003"&dt$RPID4==4]=4
dt$Pi[dt$Es=="ISL_2003"&dt$RPID4==5]=5

dt$Pi[dt$Es=="ITA_2006"&dt$RPID4==3]=1#coalition==1
dt$Pi[dt$Es=="ITA_2006"&dt$RPID4==5]=2
dt$Pi[dt$Es=="ITA_2006"&dt$RPID4==6]=3
dt$Pi[dt$Es=="ITA_2006"&dt$RPID4==4]=4
dt$Pi[dt$Es=="ITA_2006"&dt$RPID4==1]=5
dt$Pi[dt$Es=="ITA_2006"&dt$RPID4==7]=6

dt$Pi[dt$Es=="ITA_2008"&dt$RPID4==4]=1#coalition==1
dt$Pi[dt$Es=="ITA_2008"&dt$RPID4==2]=2
dt$Pi[dt$Es=="ITA_2008"&dt$RPID4==5]=3
dt$Pi[dt$Es=="ITA_2008"&dt$RPID4==6]=4
dt$Pi[dt$Es=="ITA_2008"&dt$RPID4==3]=5
dt$Pi[dt$Es=="ITA_2008"&dt$RPID4==1]=6

dt$Pi[dt$Es=="LTU_2000"&dt$RPID4==2]=1
dt$Pi[dt$Es=="LTU_2000"&dt$RPID4==3]=2
dt$Pi[dt$Es=="LTU_2000"&dt$RPID4==4]=3
dt$Pi[dt$Es=="LTU_2000"&dt$RPID4==1]=4

dt$Pi[dt$Es=="NLD_2002"&dt$RPID4==2]=1
dt$Pi[dt$Es=="NLD_2002"&dt$RPID4==6]=2
dt$Pi[dt$Es=="NLD_2002"&dt$RPID4==3]=3
dt$Pi[dt$Es=="NLD_2002"&dt$RPID4==1]=4
dt$Pi[dt$Es=="NLD_2002"&dt$RPID4==5]=5
dt$Pi[dt$Es=="NLD_2002"&dt$RPID4==7]=6

dt$Pi[dt$Es=="NOR_2001"&dt$RPID4==2]=1
dt$Pi[dt$Es=="NOR_2001"&dt$RPID4==6]=2
dt$Pi[dt$Es=="NOR_2001"&dt$RPID4==7]=3
dt$Pi[dt$Es=="NOR_2001"&dt$RPID4==1]=4
dt$Pi[dt$Es=="NOR_2001"&dt$RPID4==4]=5
dt$Pi[dt$Es=="NOR_2001"&dt$RPID4==5]=6

dt$Pi[dt$Es=="PRT_2005"&dt$RPID4==5]=1
dt$Pi[dt$Es=="PRT_2005"&dt$RPID4==4]=2
dt$Pi[dt$Es=="PRT_2005"&dt$RPID4==3]=3
dt$Pi[dt$Es=="PRT_2005"&dt$RPID4==2]=4
dt$Pi[dt$Es=="PRT_2005"&dt$RPID4==1]=5

dt$Pi[dt$Es=="SVN_2000"&dt$RPID4==2]=1
dt$Pi[dt$Es=="SVN_2000"&dt$RPID4==5]=2
dt$Pi[dt$Es=="SVN_2000"&dt$RPID4==7]=3
dt$Pi[dt$Es=="SVN_2000"&dt$RPID4==3]=4
dt$Pi[dt$Es=="SVN_2000"&dt$RPID4==6]=5
dt$Pi[dt$Es=="SVN_2000"&dt$RPID4==1]=6

dt$Pi[dt$Es=="SWE_1979"&dt$RPID4==2]=1
dt$Pi[dt$Es=="SWE_1979"&dt$RPID4==5]=2
dt$Pi[dt$Es=="SWE_1979"&dt$RPID4==3]=3
dt$Pi[dt$Es=="SWE_1979"&dt$RPID4==4]=4
dt$Pi[dt$Es=="SWE_1979"&dt$RPID4==1]=5

dt$Pi[dt$Es=="SWE_2002"&dt$RPID4==2]=1
dt$Pi[dt$Es=="SWE_2002"&dt$RPID4==5]=2
dt$Pi[dt$Es=="SWE_2002"&dt$RPID4==4]=3
dt$Pi[dt$Es=="SWE_2002"&dt$RPID4==6]=4
dt$Pi[dt$Es=="SWE_2002"&dt$RPID4==1]=5
dt$Pi[dt$Es=="SWE_2002"&dt$RPID4==3]=6

dt$Pi[dt$Es=="SWE_2010"&dt$RPID4==2]=1
dt$Pi[dt$Es=="SWE_2010"&dt$RPID4==5]=2
dt$Pi[dt$Es=="SWE_2010"&dt$RPID4==7]=3
dt$Pi[dt$Es=="SWE_2010"&dt$RPID4==4]=4
dt$Pi[dt$Es=="SWE_2010"&dt$RPID4==3]=5
dt$Pi[dt$Es=="SWE_2010"&dt$RPID4==10]=6

#common elections TEV-CSES (minus exclusions, see "check common elections"):
dt$Pi[dt$Es=="DEU_1961"&dt$RPID4==2]=1
dt$Pi[dt$Es=="DEU_1961"&dt$RPID4==1]=2
dt$Pi[dt$Es=="DEU_1961"&dt$RPID4==3]=3

dt$Pi[dt$Es=="DEU_1965"&dt$RPID4==2]=1
dt$Pi[dt$Es=="DEU_1965"&dt$RPID4==1]=2
dt$Pi[dt$Es=="DEU_1965"&dt$RPID4==3]=3

dt$Pi[dt$Es=="DEU_1976"&dt$RPID4==2]=1
dt$Pi[dt$Es=="DEU_1976"&dt$RPID4==1]=2
dt$Pi[dt$Es=="DEU_1976"&dt$RPID4==3]=3

dt$Pi[dt$Es=="DEU_1980"&dt$RPID4==2]=1
dt$Pi[dt$Es=="DEU_1980"&dt$RPID4==1]=2
dt$Pi[dt$Es=="DEU_1980"&dt$RPID4==3]=3

dt$Pi[dt$Es=="DEU_1983"&dt$RPID4==2]=1
dt$Pi[dt$Es=="DEU_1983"&dt$RPID4==1]=2
dt$Pi[dt$Es=="DEU_1983"&dt$RPID4==3]=3

dt$Pi[dt$Es=="DEU_1987"&dt$RPID4==2]=1
dt$Pi[dt$Es=="DEU_1987"&dt$RPID4==1]=2
dt$Pi[dt$Es=="DEU_1987"&dt$RPID4==3]=3

dt$Pi[dt$Es=="DEU_1990"&dt$RPID4==2]=1
dt$Pi[dt$Es=="DEU_1990"&dt$RPID4==1]=2
dt$Pi[dt$Es=="DEU_1990"&dt$RPID4==3]=3
dt$Pi[dt$Es=="DEU_1990"&dt$RPID4==5]=4

dt$Pi[dt$Es=="DEU_1994"&dt$RPID4==2]=1
dt$Pi[dt$Es=="DEU_1994"&dt$RPID4==1]=2
dt$Pi[dt$Es=="DEU_1994"&dt$RPID4==4]=3
dt$Pi[dt$Es=="DEU_1994"&dt$RPID4==3]=4
dt$Pi[dt$Es=="DEU_1994"&dt$RPID4==5]=5

dt$Pi[dt$Es=="DEU_1998"&dt$RPID4==1]=1
dt$Pi[dt$Es=="DEU_1998"&dt$RPID4==2]=2
dt$Pi[dt$Es=="DEU_1998"&dt$RPID4==4]=3
dt$Pi[dt$Es=="DEU_1998"&dt$RPID4==3]=4
dt$Pi[dt$Es=="DEU_1998"&dt$RPID4==5]=5

dt$Pi[dt$Es=="DEU_2009"&dt$RPID4==2]=1
dt$Pi[dt$Es=="DEU_2009"&dt$RPID4==1]=2
dt$Pi[dt$Es=="DEU_2009"&dt$RPID4==3]=3
dt$Pi[dt$Es=="DEU_2009"&dt$RPID4==5]=4
dt$Pi[dt$Es=="DEU_2009"&dt$RPID4==4]=5

dt$Pi[dt$Es=="DEU_2013"&dt$RPID4==2]=1
dt$Pi[dt$Es=="DEU_2013"&dt$RPID4==1]=2
dt$Pi[dt$Es=="DEU_2013"&dt$RPID4==5]=3
dt$Pi[dt$Es=="DEU_2013"&dt$RPID4==4]=4
dt$Pi[dt$Es=="DEU_2013"&dt$RPID4==3]=5

dt$Pi[dt$Es=="FIN_2007"&dt$RPID4==1]=1
dt$Pi[dt$Es=="FIN_2007"&dt$RPID4==3]=2
dt$Pi[dt$Es=="FIN_2007"&dt$RPID4==2]=3
dt$Pi[dt$Es=="FIN_2007"&dt$RPID4==4]=4
dt$Pi[dt$Es=="FIN_2007"&dt$RPID4==5]=5
dt$Pi[dt$Es=="FIN_2007"&dt$RPID4==7]=6

dt$Pi[dt$Es=="FIN_2011"&dt$RPID4==3]=1
dt$Pi[dt$Es=="FIN_2011"&dt$RPID4==2]=2
dt$Pi[dt$Es=="FIN_2011"&dt$RPID4==8]=3
dt$Pi[dt$Es=="FIN_2011"&dt$RPID4==1]=4
dt$Pi[dt$Es=="FIN_2011"&dt$RPID4==4]=5
dt$Pi[dt$Es=="FIN_2011"&dt$RPID4==5]=6

dt$Pi[dt$Es=="GRC_2009"&dt$RPID4==1]=1
dt$Pi[dt$Es=="GRC_2009"&dt$RPID4==2]=2
dt$Pi[dt$Es=="GRC_2009"&dt$RPID4==3]=3
dt$Pi[dt$Es=="GRC_2009"&dt$RPID4==4]=4
dt$Pi[dt$Es=="GRC_2009"&dt$RPID4==5]=5
dt$Pi[dt$Es=="GRC_2009"&dt$RPID4==6]=6

dt$Pi[dt$Es=="GRC_2012"&dt$RPID4==1]=1
dt$Pi[dt$Es=="GRC_2012"&dt$RPID4==2]=2
dt$Pi[dt$Es=="GRC_2012"&dt$RPID4==3]=3
dt$Pi[dt$Es=="GRC_2012"&dt$RPID4==4]=4
dt$Pi[dt$Es=="GRC_2012"&dt$RPID4==5]=5
dt$Pi[dt$Es=="GRC_2012"&dt$RPID4==6]=6

dt$Pi[dt$Es=="IRL_2007"&dt$RPID4==1]=1
dt$Pi[dt$Es=="IRL_2007"&dt$RPID4==2]=2
dt$Pi[dt$Es=="IRL_2007"&dt$RPID4==4]=3
dt$Pi[dt$Es=="IRL_2007"&dt$RPID4==3]=4
dt$Pi[dt$Es=="IRL_2007"&dt$RPID4==6]=5
dt$Pi[dt$Es=="IRL_2007"&dt$RPID4==5]=6

dt$Pi[dt$Es=="IRL_2011"&dt$RPID4==2]=1
dt$Pi[dt$Es=="IRL_2011"&dt$RPID4==3]=2
dt$Pi[dt$Es=="IRL_2011"&dt$RPID4==1]=3
dt$Pi[dt$Es=="IRL_2011"&dt$RPID4==4]=4
dt$Pi[dt$Es=="IRL_2011"&dt$RPID4==5]=5#

dt$Pi[dt$Es=="ISL_1999"&dt$RPID4==3]=1
dt$Pi[dt$Es=="ISL_1999"&dt$RPID4==1]=2
dt$Pi[dt$Es=="ISL_1999"&dt$RPID4==2]=3
dt$Pi[dt$Es=="ISL_1999"&dt$RPID4==4]=4
dt$Pi[dt$Es=="ISL_1999"&dt$RPID4==5]=5

dt$Pi[dt$Es=="ISL_2007"&dt$RPID4==3]=1
dt$Pi[dt$Es=="ISL_2007"&dt$RPID4==1]=2
dt$Pi[dt$Es=="ISL_2007"&dt$RPID4==4]=3
dt$Pi[dt$Es=="ISL_2007"&dt$RPID4==2]=4
dt$Pi[dt$Es=="ISL_2007"&dt$RPID4==5]=5
dt$Pi[dt$Es=="ISL_2007"&dt$RPID4==6]=6#

dt$Pi[dt$Es=="ISL_2009"&dt$RPID4==1]=1
dt$Pi[dt$Es=="ISL_2009"&dt$RPID4==3]=2
dt$Pi[dt$Es=="ISL_2009"&dt$RPID4==4]=3
dt$Pi[dt$Es=="ISL_2009"&dt$RPID4==2]=4
dt$Pi[dt$Es=="ISL_2009"&dt$RPID4==5]=5#
dt$Pi[dt$Es=="ISL_2009"&dt$RPID4==6]=6#

dt$Pi[dt$Es=="ISL_2013"&dt$RPID4==3]=1
dt$Pi[dt$Es=="ISL_2013"&dt$RPID4==2]=2
dt$Pi[dt$Es=="ISL_2013"&dt$RPID4==1]=3
dt$Pi[dt$Es=="ISL_2013"&dt$RPID4==4]=4
dt$Pi[dt$Es=="ISL_2013"&dt$RPID4==5]=5
dt$Pi[dt$Es=="ISL_2013"&dt$RPID4==6]=6

dt$Pi[dt$Es=="NLD_1994"&dt$RPID4==1]=1
dt$Pi[dt$Es=="NLD_1994"&dt$RPID4==2]=2
dt$Pi[dt$Es=="NLD_1994"&dt$RPID4==3]=3
dt$Pi[dt$Es=="NLD_1994"&dt$RPID4==4]=4

dt$Pi[dt$Es=="NLD_1998"&dt$RPID4==1]=1#EV 4 parties, CSES 6 parties coded
dt$Pi[dt$Es=="NLD_1998"&dt$RPID4==3]=2
dt$Pi[dt$Es=="NLD_1998"&dt$RPID4==2]=3
dt$Pi[dt$Es=="NLD_1998"&dt$RPID4==4]=4
dt$Pi[dt$Es=="NLD_1998"&dt$RPID4==5]=5
dt$Pi[dt$Es=="NLD_1998"&dt$RPID4==7]=6#

dt$Pi[dt$Es=="NOR_1981"&dt$RPID4==2]=1
dt$Pi[dt$Es=="NOR_1981"&dt$RPID4==6]=2
dt$Pi[dt$Es=="NOR_1981"&dt$RPID4==4]=3
dt$Pi[dt$Es=="NOR_1981"&dt$RPID4==5]=4
dt$Pi[dt$Es=="NOR_1981"&dt$RPID4==1]=5
dt$Pi[dt$Es=="NOR_1981"&dt$RPID4==7]=6

dt$Pi[dt$Es=="NOR_1985"&dt$RPID4==2]=1
dt$Pi[dt$Es=="NOR_1985"&dt$RPID4==6]=2
dt$Pi[dt$Es=="NOR_1985"&dt$RPID4==4]=3
dt$Pi[dt$Es=="NOR_1985"&dt$RPID4==5]=4
dt$Pi[dt$Es=="NOR_1985"&dt$RPID4==1]=5
dt$Pi[dt$Es=="NOR_1985"&dt$RPID4==7]=6

dt$Pi[dt$Es=="NOR_1989"&dt$RPID4==2]=1
dt$Pi[dt$Es=="NOR_1989"&dt$RPID4==6]=2
dt$Pi[dt$Es=="NOR_1989"&dt$RPID4==7]=3
dt$Pi[dt$Es=="NOR_1989"&dt$RPID4==1]=4
dt$Pi[dt$Es=="NOR_1989"&dt$RPID4==4]=5
dt$Pi[dt$Es=="NOR_1989"&dt$RPID4==5]=6

dt$Pi[dt$Es=="NOR_1993"&dt$RPID4==2]=1
dt$Pi[dt$Es=="NOR_1993"&dt$RPID4==5]=2
dt$Pi[dt$Es=="NOR_1993"&dt$RPID4==6]=3
dt$Pi[dt$Es=="NOR_1993"&dt$RPID4==1]=4
dt$Pi[dt$Es=="NOR_1993"&dt$RPID4==4]=5
dt$Pi[dt$Es=="NOR_1993"&dt$RPID4==7]=6

dt$Pi[dt$Es=="NOR_1997"&dt$RPID4==2]=1
dt$Pi[dt$Es=="NOR_1997"&dt$RPID4==7]=2
dt$Pi[dt$Es=="NOR_1997"&dt$RPID4==4]=3
dt$Pi[dt$Es=="NOR_1997"&dt$RPID4==6]=4
dt$Pi[dt$Es=="NOR_1997"&dt$RPID4==5]=5
dt$Pi[dt$Es=="NOR_1997"&dt$RPID4==1]=6

dt$Pi[dt$Es=="NOR_2005"&dt$RPID4==2]=1
dt$Pi[dt$Es=="NOR_2005"&dt$RPID4==7]=2
dt$Pi[dt$Es=="NOR_2005"&dt$RPID4==6]=3
dt$Pi[dt$Es=="NOR_2005"&dt$RPID4==1]=4
dt$Pi[dt$Es=="NOR_2005"&dt$RPID4==4]=5
dt$Pi[dt$Es=="NOR_2005"&dt$RPID4==5]=6

dt$Pi[dt$Es=="PRT_2002"&dt$RPID4==4]=1
dt$Pi[dt$Es=="PRT_2002"&dt$RPID4==5]=2
dt$Pi[dt$Es=="PRT_2002"&dt$RPID4==2]=3
dt$Pi[dt$Es=="PRT_2002"&dt$RPID4==3]=4
dt$Pi[dt$Es=="PRT_2002"&dt$RPID4==1]=5
dt$Pi[dt$Es=="PRT_2002"&dt$RPID4==6]=6

dt$Pi[dt$Es=="PRT_2009"&dt$RPID4==5]=1#osym4-5  inverted!?
dt$Pi[dt$Es=="PRT_2009"&dt$RPID4==4]=2
dt$Pi[dt$Es=="PRT_2009"&dt$RPID4==2]=3
dt$Pi[dt$Es=="PRT_2009"&dt$RPID4==1]=4
dt$Pi[dt$Es=="PRT_2009"&dt$RPID4==3]=5

dt$Pi[dt$Es=="SWE_1982"&dt$RPID4==2]=1
dt$Pi[dt$Es=="SWE_1982"&dt$RPID4==5]=2
dt$Pi[dt$Es=="SWE_1982"&dt$RPID4==3]=3
dt$Pi[dt$Es=="SWE_1982"&dt$RPID4==4]=4
dt$Pi[dt$Es=="SWE_1982"&dt$RPID4==1]=5

dt$Pi[dt$Es=="SWE_1985"&dt$RPID4==2]=1
dt$Pi[dt$Es=="SWE_1985"&dt$RPID4==5]=2
dt$Pi[dt$Es=="SWE_1985"&dt$RPID4==4]=3
dt$Pi[dt$Es=="SWE_1985"&dt$RPID4==3]=4
dt$Pi[dt$Es=="SWE_1985"&dt$RPID4==1]=5
dt$Pi[dt$Es=="SWE_1985"&dt$RPID4==6]=6

dt$Pi[dt$Es=="SWE_1988"&dt$RPID4==2]=1
dt$Pi[dt$Es=="SWE_1988"&dt$RPID4==5]=2
dt$Pi[dt$Es=="SWE_1988"&dt$RPID4==4]=3
dt$Pi[dt$Es=="SWE_1988"&dt$RPID4==3]=4
dt$Pi[dt$Es=="SWE_1988"&dt$RPID4==1]=5

dt$Pi[dt$Es=="SWE_1991"&dt$RPID4==2]=1
dt$Pi[dt$Es=="SWE_1991"&dt$RPID4==5]=2
dt$Pi[dt$Es=="SWE_1991"&dt$RPID4==4]=3
dt$Pi[dt$Es=="SWE_1991"&dt$RPID4==3]=4
dt$Pi[dt$Es=="SWE_1991"&dt$RPID4==6]=5
dt$Pi[dt$Es=="SWE_1991"&dt$RPID4==9]=6

dt$Pi[dt$Es=="SWE_1994"&dt$RPID4==2]=1
dt$Pi[dt$Es=="SWE_1994"&dt$RPID4==5]=2
dt$Pi[dt$Es=="SWE_1994"&dt$RPID4==3]=3
dt$Pi[dt$Es=="SWE_1994"&dt$RPID4==4]=4
dt$Pi[dt$Es=="SWE_1994"&dt$RPID4==1]=5
dt$Pi[dt$Es=="SWE_1994"&dt$RPID4==7]=6

dt$Pi[dt$Es=="SWE_1998"&dt$RPID4==2]=1
dt$Pi[dt$Es=="SWE_1998"&dt$RPID4==5]=2
dt$Pi[dt$Es=="SWE_1998"&dt$RPID4==1]=3
dt$Pi[dt$Es=="SWE_1998"&dt$RPID4==6]=4
dt$Pi[dt$Es=="SWE_1998"&dt$RPID4==3]=5
dt$Pi[dt$Es=="SWE_1998"&dt$RPID4==4]=6

dt$Pi[dt$Es=="SWE_2006"&dt$RPID4==2]=1
dt$Pi[dt$Es=="SWE_2006"&dt$RPID4==5]=2
dt$Pi[dt$Es=="SWE_2006"&dt$RPID4==3]=3
dt$Pi[dt$Es=="SWE_2006"&dt$RPID4==4]=4
dt$Pi[dt$Es=="SWE_2006"&dt$RPID4==6]=5
dt$Pi[dt$Es=="SWE_2006"&dt$RPID4==1]=6

dt$Pi[dt$RPID1!=2]=NA#if R does not identify with any party


#EeconS (economy subjective retrospective sociotropic):----

colnames(dt)[colnames(dt)=="RRSEE"]="EeconS"
dt$EeconS[dt$EeconS>1]=NA
dt$EeconS=as.numeric(dt$EeconS)

# 0. Negative evaluation
# 1. Positive evaluation

# #TEV recoded CSES EeconS from c(1,2,3,4,5) to c(1, 0.75, 0.5, 0.25, 0):
# dt_=dt[dt$Es=="DEU_2013",]
# dt_$EeconS[1:20]
# table(dt_$EeconS)
# rm(dt_)


#PrePost:----
colnames(dt)[colnames(dt)=="PREPOST"]="PrePost"
dt$PrePost=as.numeric(dt$PrePost)

# 1. Post-election study
# 2. Pre-election study
# 3. Panel, with some pre-election and some post-election data

# table(d$Es,dt$Ed)# code prepost only for EV in de dataset
#           CSES EV-LP   TEV
# AUS_1993     0 18138     0#post#
# CAN_1968     0 16602     0#post#
# CAN_1974     0 15372     0#post#
# CAN_1979     0 16482     0#post#
# CAN_1988     0 17538     0#panel#
# CAN_1993     0 20040     0#panel#
# DNK_1971     0  7812     0#NA#
# DNK_1973     0  3198     0#NA#
# DNK_1994     0 12126     0#NA#
# GBR_1974     0 28962     0#post#
# GBR_1979     0 11358     0#post#
# NLD_1986     0  9780     0#panel#


#Media use:----

dt$Mnwsp=dt$RMEDUS1#newspaper use
dt$Mtele=dt$RMEDUS2#television use
dt$Mradi=dt$RMEDUS3#radio use
dt$Minte=dt$RMEDUS4#internet use

dt$Mnwsp[dt$Mnwsp>1]=NA
dt$Mtele[dt$Mtele>1]=NA
dt$Mradi[dt$Mradi>1]=NA
dt$Minte[dt$Minte>1]=NA

  
#fix IRL_2007:----

#read dtI (TEV), ddI (CSES), and di (NES) for IRL_2007:
dtI=dt[dt$Es=="IRL_2007",]
ddI=dew[dew$Es=="IRL_2007",]
di=read.dta("INESWide_Beta.dta")

#Va corresponds in aggregate but not in the order:
table(dtI$Va)
table(ddI$Va)
summary(dtI$Va-ddI$Va)

#order the datasets by their IDs:
ddI=ddI[order(ddI$Rid),]
dtI=dtI[order(dtI$ORESPID),]
di=di[order(di$id),]

#even after ordering by IDS, Va does not correspond in the order:
summary(dtI$Va-ddI$Va)

#IDs do not correspond between datasets :
length(sort(unique(dtI$ORESPID)))
sort(unique(dtI$ORESPID))[1:20]
dtI$RRESPID=as.character(dtI$RRESPID)
sort(unique(dtI$RRESPID))[1:20]
length(sort(unique(ddI$Rid)))
sort(unique(ddI$Rid))[1:20]
length(sort(unique(di$id)))
sort(unique(di$id))[1:20]

#CSES and TEV have same aggregate Pl.i:
table(ddI$Pl.1)
table(dtI$Pl.1)
table(ddI$Pl.2)
table(dtI$Pl.2)
table(ddI$Pl.3)
table(dtI$Pl.3)
table(ddI$Pl.4)
table(dtI$Pl.4)
table(ddI$Pl.5)
table(dtI$Pl.5)
table(ddI$Pl.6)
table(dtI$Pl.6)

#CSES and TEV have same aggregate Ll.i BUT for Ll.1:
table(ddI$Ll.1)#Fianna Fail (FF) - Bertie Ahern (FF) [CSES imdb]
table(dtI$Ll.1)#Fiánna Fail (FF) / Bertie Ahern
table(ddI$Ll.2)
table(dtI$Ll.2)
table(ddI$Ll.3)
table(dtI$Ll.3)
table(ddI$Ll.4)
table(dtI$Ll.4)
table(ddI$Ll.5)
table(dtI$Ll.5)
table(ddI$Ll.6)
table(dtI$Ll.6)

#the NES for Ll.1 is the same than CSES:
table(di$v0521_07)

#By putting together all the variables with same aggregate values between CSES and TEV it is
# not possible to uniquely identify respondents also adding,LRR,RREGION,RAGE,RGENDER
# (not possible to add Rage as it is not the same cumulatively):
table(dtI$Rage)
table(ddI$Rage)
dtI$match=paste0(dtI$RREGION,"_",dtI$RGENDER,"_",dtI$Va,"_",dtI$LRR,"_",dtI$Pl.1,"_",dtI$Pl.2,"_",dtI$Pl.3,"_",dtI$Pl.4,"_",dtI$Pl.5,"_",dtI$Pl.6
                 ,dtI$Ll.2,"_",dtI$Ll.3,"_",dtI$Ll.4,"_",dtI$Ll.5,"_",dtI$Ll.6)
length(sort(unique(dtI$match)))
nrow(dtI)

#therefore I keep only the data in the CSES for IRL_2007, which at the end only causes to
# loose the data regarding media use:
dt=dt[!(dt$Es %in% c("IRL_2007")),]#exclude IRL_2007 from TEV

rm(dtI,ddI,di)#removing working variables


#fix NOR_2001:----

#read dtN (TEV) and ddN (NES) for NOR_2001:
dtN=dt[dt$Es=="NOR_2001",]
ddN=read.dta("NSD0663.dta")

#order the datasets by their IDs:
ddN=ddN[order(ddN$v001),]
dtN=dtN[order(dtN$ORESPID),]

#Va:v380
sort(table(ddN$v380))#vote choice:
class(ddN$v380)
# 3 Labour party (DnA)
# 8 Conservative Party (H)
# 9 Progress Party (FrP)
# 2 Socialist Left Party (SV)
# 5 Christian Democratic Party (KrF)
# 6 Center Party (Sp)
ddN$Va=NA
ddN$Va[ddN$v380==3]=1
ddN$Va[ddN$v380==8]=2
ddN$Va[ddN$v380==9]=3
ddN$Va[ddN$v380==2]=4
ddN$Va[ddN$v380==5]=5
ddN$Va[ddN$v380==6]=6
# dtN$Va[dtN$Es=="NOR_2001"&dtN$Vn==2]=1#code Va for dtN:
# dtN$Va[dtN$Es=="NOR_2001"&dtN$Vn==6]=2
# dtN$Va[dtN$Es=="NOR_2001"&dtN$Vn==7]=3
# dtN$Va[dtN$Es=="NOR_2001"&dtN$Vn==1]=4
# dtN$Va[dtN$Es=="NOR_2001"&dtN$Vn==4]=5
# dtN$Va[dtN$Es=="NOR_2001"&dtN$Vn==5]=6
summary(dtN$Va-ddN$Va)#vote choice is the same between TEV and Norway: it corresponds Id by Id!!!

#Rage:
ddN$Rage=ddN$v003
ddN$Rage[ddN$Rage==17]=18#Norway NES coded age also 17 while TEV puts it in 18
summary(dtN$Rage-ddN$Rage)#also it corresponds ID by ID!!!

#LRR:
ddN$LRR=ddN$v316
ddN$LRR=as.numeric(ddN$LRR)#rescaling 0-10 scale
ddN$LRR=ddN$LRR-1
ddN$LRR[ddN$LRR>10]=NA
summary(dtN$LRR-ddN$LRR)#also it corresponds ID by ID!!!

#Media use:
#there is no data on Media use: summary(dtN$RMEDUS4)

#EeconS: (it is pre-election in the dataset)
#v095:
# 1 better
# 2 same
# 3 worse
#v096:
# 1 much better
# 2 a bit better
#v097:
# 1 much worse
# 2 a bit worse
ddN$v095=as.numeric(ddN$v095)#recode 1:5 scale CSES: 1. GOTTEN BETTER; 5. GOTTEN WORSE
ddN$v096=as.numeric(ddN$v096)
ddN$v097=as.numeric(ddN$v097)
ddN$v095[ddN$v095>5]=NA#coding missing as NAs
ddN$v096[ddN$v096>2]=NA#coding missing as NAs
ddN$v097[ddN$v097>2]=NA#coding missing as NAs
ddN$v097[ddN$v097==2]=4
ddN$v097[ddN$v097==1]=5
ddN$EeconS=NA
ddN$EeconS=ifelse(ddN$v095<3, ddN$v096,
                  (ifelse(ddN$v095==3, 3,
                          (ifelse(ddN$v095>3,ddN$v097,ddN$EeconS)))))
ddN$EeconS[ddN$EeconS==1]=1#recode to 0-1 scale TEV: 0. Negative evaluation; 1. Positive evaluation
ddN$EeconS[ddN$EeconS==2]=0.75
ddN$EeconS[ddN$EeconS==3]=0.5
ddN$EeconS[ddN$EeconS==4]=0.25
ddN$EeconS[ddN$EeconS==5]=0
# colnames(dtN)[colnames(dtN)=="RRSEE"]="EeconS"
# dtN$EeconS[dtN$EeconS>1]=NA
# dtN$EeconS=as.numeric(dtN$EeconS)
summary(dtN$EeconS-dtN$EeconS)#also it corresponds ID by ID!!!

#however:
#Pl is out:
ddN$Pl.1=ddN$v300#Pl Labour Party
ddN$Pl.2=ddN$v299#Pl Conservative Party
ddN$Pl.3=ddN$v301#Pl Progress Party
ddN$Pl.4=ddN$v298#Pl Socialist Left
ddN$Pl.5=ddN$v295#Pl Christian Dem
ddN$Pl.6=ddN$v297#Pl Center Party
# Pl 0.49624   1.64253  0.01863   0.01816 27.321 <2e-16
# Ll 0.15829   1.17151  0.01837   0.01812  8.735 <2e-16
ddN$Pl.1=as.numeric(ddN$Pl.1)#rescaling 0-10 scale
ddN$Pl.2=as.numeric(ddN$Pl.2)
ddN$Pl.3=as.numeric(ddN$Pl.3)
ddN$Pl.4=as.numeric(ddN$Pl.4)
ddN$Pl.5=as.numeric(ddN$Pl.5)
ddN$Pl.6=as.numeric(ddN$Pl.6)
ddN$Pl.1=ddN$Pl.1-1
ddN$Pl.2=ddN$Pl.2-1
ddN$Pl.3=ddN$Pl.3-1
ddN$Pl.4=ddN$Pl.4-1
ddN$Pl.5=ddN$Pl.5-1
ddN$Pl.6=ddN$Pl.6-1
ddN$Pl.1[ddN$Pl.1>10]=NA
ddN$Pl.2[ddN$Pl.2>10]=NA
ddN$Pl.3[ddN$Pl.3>10]=NA
ddN$Pl.4[ddN$Pl.4>10]=NA
ddN$Pl.5[ddN$Pl.5>10]=NA
ddN$Pl.6[ddN$Pl.6>10]=NA

#Ll is out:
ddN$Ll.1=ddN$v322#Ll Jens Stoltenberg Labour Party
ddN$Ll.2=ddN$v321#Ll Jan Petersen Conservative Party
ddN$Ll.3=ddN$v323#Ll Carl I. Hagen Progress Party
ddN$Ll.4=ddN$v320#Ll Kristin Halvorsen Socialist Left
ddN$Ll.5=ddN$v317#Ll Valgerd Svarstad Haugland Christian Dem
ddN$Ll.6=ddN$v319#Ll Odd Roger Enoksen Center Party
# ddN$v086#Ll Thorbjørn Jagland
# ddN$v078#Ll Lars Sponheim
# ddN$v084#Ll Aslak Sira Myhre
# ddN$v085#Ll Kjell Magne Bondevik
ddN$Ll.1=as.numeric(ddN$Ll.1)#rescaling 0-10 scale
ddN$Ll.2=as.numeric(ddN$Ll.2)
ddN$Ll.3=as.numeric(ddN$Ll.3)
ddN$Ll.4=as.numeric(ddN$Ll.4)
ddN$Ll.5=as.numeric(ddN$Ll.5)
ddN$Ll.6=as.numeric(ddN$Ll.6)
ddN$Ll.1=ddN$Ll.1-1
ddN$Ll.2=ddN$Ll.2-1
ddN$Ll.3=ddN$Ll.3-1
ddN$Ll.4=ddN$Ll.4-1
ddN$Ll.5=ddN$Ll.5-1
ddN$Ll.6=ddN$Ll.6-1
ddN$Ll.1[ddN$Ll.1>10]=NA
ddN$Ll.2[ddN$Ll.2>10]=NA
ddN$Ll.3[ddN$Ll.3>10]=NA
ddN$Ll.4[ddN$Ll.4>10]=NA
ddN$Ll.5[ddN$Ll.5>10]=NA
ddN$Ll.6[ddN$Ll.6>10]=NA

#Pl and Ll are completely out:
table(dtN$Pl.1)
table(ddN$Pl.1)
table(dtN$Pl.2)
table(ddN$Pl.2)
table(dtN$Pl.3)
table(ddN$Pl.3)
table(dtN$Pl.4)
table(ddN$Pl.4)
table(dtN$Pl.5)
table(ddN$Pl.5)
table(dtN$Pl.6)
table(ddN$Pl.6)
table(dtN$Ll.1)
table(ddN$Ll.1)
table(dtN$Ll.2)
table(ddN$Ll.2)
table(dtN$Ll.3)
table(ddN$Ll.3)
table(dtN$Ll.4)
table(ddN$Ll.4)
table(dtN$Ll.5)
table(ddN$Ll.5)
table(dtN$Ll.6)
table(ddN$Ll.6)

#LRP.i is also out:
ddN$LRP.1=ddN$v308#Pl Labour Party
ddN$LRP.2=ddN$v307#Pl Conservative Party
ddN$LRP.3=ddN$v309#Pl Progress Party
ddN$LRP.4=ddN$v306#Pl Socialist Left
ddN$LRP.5=ddN$v303#Pl Christian Dem
ddN$LRP.6=ddN$v305#Pl Center Party
ddN$LRP.1=as.numeric(ddN$LRP.1)#rescaling 0-10 scale
ddN$LRP.2=as.numeric(ddN$LRP.2)
ddN$LRP.3=as.numeric(ddN$LRP.3)
ddN$LRP.4=as.numeric(ddN$LRP.4)
ddN$LRP.5=as.numeric(ddN$LRP.5)
ddN$LRP.6=as.numeric(ddN$LRP.6)
ddN$LRP.1=ddN$LRP.1-1
ddN$LRP.2=ddN$LRP.2-1
ddN$LRP.3=ddN$LRP.3-1
ddN$LRP.4=ddN$LRP.4-1
ddN$LRP.5=ddN$LRP.5-1
ddN$LRP.6=ddN$LRP.6-1
ddN$LRP.1[ddN$LRP.1>10]=NA
ddN$LRP.2[ddN$LRP.2>10]=NA
ddN$LRP.3[ddN$LRP.3>10]=NA
ddN$LRP.4[ddN$LRP.4>10]=NA
ddN$LRP.5[ddN$LRP.5>10]=NA
ddN$LRP.6[ddN$LRP.6>10]=NA
summary(dtN$LRP.1-ddN$LRP.1)#it does not correspond ID by ID!!!

#therefore I need to correct Pl.i, Ll.i, and LRP.i:
# I can just copy them since I ordered by ID and all the other variables are the same ID by ID
# and before I  just need to order dt by Es and ID:
dt=dt[order(dt$Es,dt$ORESPID),]
summary(dt$Va[dt$Es=="NOR_2001"]-ddN$Va)#check the order is correct!
dt$Pl.1[dt$Es=="NOR_2001"]=ddN$Pl.1#assign correct values:
dt$Pl.2[dt$Es=="NOR_2001"]=ddN$Pl.2
dt$Pl.3[dt$Es=="NOR_2001"]=ddN$Pl.3
dt$Pl.4[dt$Es=="NOR_2001"]=ddN$Pl.4
dt$Pl.5[dt$Es=="NOR_2001"]=ddN$Pl.5
dt$Pl.6[dt$Es=="NOR_2001"]=ddN$Pl.6
dt$Ll.1[dt$Es=="NOR_2001"]=ddN$Ll.1
dt$Ll.2[dt$Es=="NOR_2001"]=ddN$Ll.2
dt$Ll.3[dt$Es=="NOR_2001"]=ddN$Ll.3
dt$Ll.4[dt$Es=="NOR_2001"]=ddN$Ll.4
dt$Ll.5[dt$Es=="NOR_2001"]=ddN$Ll.5
dt$Ll.6[dt$Es=="NOR_2001"]=ddN$Ll.6
dt$LRP.1[dt$Es=="NOR_2001"]=ddN$LRP.1
dt$LRP.2[dt$Es=="NOR_2001"]=ddN$LRP.2
dt$LRP.3[dt$Es=="NOR_2001"]=ddN$LRP.3
dt$LRP.4[dt$Es=="NOR_2001"]=ddN$LRP.4
dt$LRP.5[dt$Es=="NOR_2001"]=ddN$LRP.5
dt$LRP.6[dt$Es=="NOR_2001"]=ddN$LRP.6

rm(dtN,ddN)#removing working variables


#dataset common elections:----

#keep only common elections between CSES and TEV:
dt_=dt[(dt$Es %in% c(sort(unique(dew$Es)))),]#common elections
dew_=dew[(dew$Es %in% c(sort(unique(dt$Es)))),]#common elections

#keep only variables of interest:
dt_=dt_[,names(dt_) %in% c("Ec","Ey","Es","NP","Rwd","Vt","Va","Pl.1","Pl.2","Pl.3","Pl.4","Pl.5","Pl.6"
                           ,"Ll.1","Ll.2","Ll.3","Ll.4","Ll.5","Ll.6"
                           ,"LRR","LRP.1","LRP.2","LRP.3","LRP.4","LRP.5","LRP.6")]
dew_=dew_[,names(dew_) %in% c("Ec","Ey","Es","NP","Rwd","Vt","Va","Pl.1","Pl.2","Pl.3","Pl.4","Pl.5","Pl.6"
                              ,"Ll.1","Ll.2","Ll.3","Ll.4","Ll.5","Ll.6"
                              ,"LRR","LRP.1","LRP.2","LRP.3","LRP.4","LRP.5","LRP.6")]

#add dataset dummy:
dt_$Ed=0#TEV (analyzed elections)
dew_$Ed=1#CSES-EV-LP (non-analyzed elections)

#stack:
dd=rbind(dt_,dew_)

#create long datasets and add relevant variables:
dd=as.data.frame(dd)
dd=mlogit.data(dd, varying=c(7:24), choice="Va", shape="wide")#create long dtt
dd$LRD=abs(dd$LRR-dd$LRP)#Left-Right Distance voter-party
dd$time=dd$Ey-1960
dd$Esalt=paste0(dd$Es,dd$alt)

#save:
save(dd, file = "d29_doubleEs.RData")


#main dataset:----

#keep in CSES-EV-LP only elections not already present in TEV:
dew=dew[!(dew$Es %in% c(sort(unique(dt$Es)))),]

# #keep in TEV only elections not already present in CSES-EV-LP:
# dt=dt[!(dt$Es %in% c(sort(unique(dew$Es)))),]

#keep only variables of interest:
dt=dt[,names(dt) %in% c(names(dew),"Mnwsp","Mtele","Mradi","Minte")]

#code Ed election dataset:
dt$Ed="TEV"
# dew$Ed[dew$Ey<1996]="EV-LP"
# dew$Ed[dew$Ey>=1996]="CSES"

#code NAs for media use in dew:
dew$Mnwsp=NA
dew$Mradi=NA
dew$Mtele=NA
dew$Minte=NA

#remove non relevant columns dew:
# dew=dew[, !names(dew) %in% c("Rid","CMPid.1","CMPid.2","CMPid.3","CMPid.4","CMPid.5","CMPid.6")]
dew=dew[, !names(dew) %in% c("Rid")]

names(dew)
names(dt)
#reorder dt columns:
dt=dt[,c(names(dew))]

#merge dew and dt:
de=rbind(dew,dt)

#exclude data that will not enter in the analysis to limit memory use:
de=de[!is.na(de$Va),]#remove respondents without vote choice
Pl=de[, names(de) %in% c(paste0("Pl.", 1:6))]#keep only respondents (and elections) with coded Pl,Ll (for at least one party)
Pl=as.matrix(Pl)
de=de[rowMins(Pl, na.rm=T)<20,]
Ll=de[, names(de) %in% c(paste0("Ll.", 1:6))]
Ll=as.matrix(Ll)
de=de[rowMins(Ll, na.rm=T)<20,]
rm(Pl,Ll)
de_temp=de[!duplicated(de$Es),]#remove countries with only one election coded (they will not generate any time trend)
names(which(table(de_temp$Ec)==1))#"ARG" "BGR" "EST" "HRV" "LTU" "MNE" "SRB" "TWN"
de=de[!(de$Ec %in% c(names(which(table(de_temp$Ec)==1)))),]
rm(de_temp)

#subset without Eastern Europe:
de=de[!(de$Ec %in% c("CZE","HUN","LVA","POL","SVK","SVN","ZAF")),]#exclude non democracies before 1992 (mainly Eastern Europe)

de3=de#temp dataset for only top three parties

names(de)
# [1] "Ec"          "Ey"          "Es"          "Ed"          "Rwd"         "Rage"        "NP"         
# [8] "CoalCode"    "Vt"          "Va"          "Pl.1"        "Pl.2"        "Pl.3"        "Pl.4"       
# [15] "Pl.5"        "Pl.6"        "Ll.1"        "Ll.2"        "Ll.3"        "Ll.4"        "Ll.5"       
# [22] "Ll.6"        "LRR"         "LRP.1"       "LRP.2"       "LRP.3"       "LRP.4"       "LRP.5"      
# [29] "LRP.6"       "EeconS"      "P1P2rat"     "GDM"         "VPf"         "PrePost"     "Pv.1"       
# [36] "Pv.2"        "Pv.3"        "Pv.4"        "Pv.5"        "Pv.6"        "Ps.1"        "Ps.2"       
# [43] "Ps.3"        "Ps.4"        "Ps.5"        "Ps.6"        "CMPrile.1"   "CMPrile.2"   "CMPrile.3"  
# [50] "CMPrile.4"   "CMPrile.5"   "CMPrile.6"   "CMPid.1"     "CMPid.2"     "CMPid.3"     "CMPid.4"    
# [57] "CMPid.5"     "CMPid.6"     "CMPper415.1" "CMPper415.2" "CMPper415.3" "CMPper415.4" "CMPper415.5"
# [64] "CMPper415.6" "Pi"          "Mnwsp"       "Mradi"       "Mtele"       "Minte"   
de=mlogit.data(de, varying=c(11:22,24:29,35:64), choice="Va", shape="wide")#create long de:

#recode Pi into 0-1:
de$Pi[de$Pi==de$alt]=0.5
de$Pi[de$Pi!=de$alt&de$Pi!=0.5]=0
de$Pi[de$Pi==0.5]=1

#code other variables:
de$LRD=abs(de$LRR-de$LRP)#Left-Right Distance voter-party
de$time=de$Ey-1960
de$Esalt=paste0(de$Es,de$alt)

#save:
setwd("~/Dropbox/Princeton/P29 reassessing leaders effect/_data")
save(de, file = "d29.RData")


#dataset only top 3 parties:----

de=de3
de=de[de$Va %in% c(1,2,3),-which(names(de) %in% c("Pl.4","Pl.5","Pl.6","Ll.4","Ll.5","Ll.6"
        ,"LRP.4","LRP.5","LRP.6","Pv.4","Pv.5","Pv.6","Ps.4","Ps.5","Ps.6"
        ,"CMPrile.4","CMPrile.5","CMPrile.6"
        ,"CMPid.4","CMPid.5","CMPid.6","CMPper415.4","CMPper415.5","CMPper415.6"))]
names(de)
# [1] "Ec"          "Ey"          "Es"          "Ed"          "Rwd"         "Rage"        "NP"         
# [8] "CoalCode"    "Vt"          "Va"          "Pl.1"        "Pl.2"        "Pl.3"        "Ll.1"       
# [15] "Ll.2"        "Ll.3"        "LRR"         "LRP.1"       "LRP.2"       "LRP.3"       "EeconS"     
# [22] "P1P2rat"     "GDM"         "VPf"         "PrePost"     "Pv.1"        "Pv.2"        "Pv.3"       
# [29] "Ps.1"        "Ps.2"        "Ps.3"        "CMPrile.1"   "CMPrile.2"   "CMPrile.3"   "CMPid.1"    
# [36] "CMPid.2"     "CMPid.3"     "CMPper415.1" "CMPper415.2" "CMPper415.3" "Pi"          "Mnwsp"      
# [43] "Mradi"       "Mtele"       "Minte" 
de=mlogit.data(de, varying=c(11:16,18:20,26:40), choice="Va", shape="wide")#create long de:
#code other variables:
de$LRD=abs(de$LRR-de$LRP)#Left-Right Distance voter-party
de$time=de$Ey-1960
de$Esalt=paste0(de$Es,de$alt)
#save:
setwd("~/Dropbox/Princeton/P29 reassessing leaders effect/_data")
save(de, file = "d29_NP3.RData")

